Member specialization for a template class for type groups?

I have a matrix class, and I want to print the matrix on the terminal differently for different types of matrices (int, float, double). I want to achieve this:

  • If the matrix type is if int, a printed matrix usingprintf("%d ",matrix[i][j])
  • If the matrix type is if floator double, print the matrix withprintf("%.3f ",matrix[i][j])
  • Otherwise, enter an error

Here are the relevant parts of what I have:

...

template <class T>
class Matrix2D {
private:
    std::vector< std::vector<T> > matrix;
public:
    ...
    void print() const; // print the whole matrix
}

...

template <class T>
void Matrix2D<T>::print() const {
    // throw an error
}

template <>
void Matrix2D<int>::print() const {
    // print matrix using printf("%d ",matrix[i][j])
}

template <>
void Matrix2D<float,double>::print() const {
    // print matrix using printf("%.3f ",matrix[i][j])
}

But with the help Matrix2D<float,double>an error message appears error: wrong number of template arguments (2, should be 1). However, I want to have a common function print()for matrices like floatand double(I don't want to copy the same thing twice). What is the easiest way to achieve this? Thank!

+4
3

, :

public:
    void print() const
    {
        for (auto const& row : matrix)
            for (auto const& v : row)
                print(v);
    }

private:
    static void print(int val)
    {
        printf("%d ", val);
    }

    static void print(float val)
    {
        printf("%.3f", val);
    }
+2

Matrix2D<short>::print() . .

-

, .

namespace detail
{

struct int_tag{};
struct float_tag{};
struct error_tag{};

template<typename T> struct choose_tag { using type = error_tag; };

template<> struct choose_tag<int> { using type = int_tag; };
template<> struct choose_tag<double> { using type = float_tag; };
template<> struct choose_tag<float> { using type = float_tag; };

template<typename T>
using tag = typename choose_tag<T>::type;

}

-

Boost.Hana( MPL). :

template<typename T, typename... Us>
constexpr bool contains =
        hana::any_of(hana::tuple_t<Us...>, hana::partial(hana::equal, hana::type_c<T>))();

enable_if :

template<typename T, typename = std::void_t<>>
struct choose_tag
{ using type = error_tag; };

template<typename T>
struct choose_tag<T, enable_if_t<contains<T, int>>>
{ using type = int_tag; };

template<typename T>
struct choose_tag<T, enable_if_t<contains<T, double, float>>>
{ using type = float_tag; };

- print

, :

template<typename T>
void print_matrix(detail::int_tag, T&&) {
    cout << __PRETTY_FUNCTION__ << endl;
}

template<typename T>
void print_matrix(detail::float_tag, T&&) {
    cout << __PRETTY_FUNCTION__ << endl;
}

template<typename T>
void print_matrix(detail::error_tag, T&&) {
    cout << __PRETTY_FUNCTION__ << endl;
}

:

template <class T>
class Matrix2D {
private:
    std::vector< std::vector<T> > matrix;
public:

    void print() const {
        print_matrix(detail::tag<T>{}, *this);
    }
};

.

+1

You can use enable_ifand property type , do something like this:

template<class T> class MyClass
{
public:
    // this one will be created if the argument is of a floating point type, i.e.
    // double or float
    template<typename U = T>
    typename std::enable_if<std::is_floating_point<U>::value, void>::type
    print(U v)
    {
        std::cout << "float" << std::endl;
    }

    // this one will be created if the argument is of an integral type, i.e.
    // bool, char, char16_t, char32_t, wchar_t, short, int, long, long long
    template<typename U = T>
    typename std::enable_if<std::is_integral<U>::value, void>::type
    print(U v)
    {
        std::cout << "integer" << std::endl;
    }

};


int main() {
    MyClass<int>c;
    c.print(1);
    c.print(1.f);
}

Conclusion:

integer
float
0
source

Source: https://habr.com/ru/post/1657263/


All Articles