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);
}
};
.