, , user463035818 answer ( ) , tagged dispatch.
, .
, Eigen::Array<double, rows, cols>:
template<unsigned int rows, unsigned int cols,typename arrtype>
class Variance
{
public:
double f(const arrtype& arg)
{
return f_impl(arg, tag<arrtype>{});
}
private:
template<class... T>
struct tag{};
template<class... T>
double f_impl(const arrtype&, tag<T...>){
std::cout << "catch-all function\n";
return 42.0;
}
double f_impl(const arrtype&, tag<Eigen::Array<double, rows, cols>>){
std::cout << "specialization for Eigen::Array<double, rows, cols>\n";
return 1337.0;
}
};
:
Variance<1, 1, int> non_specialized;
non_specialized.f(int{}); // prints "catch-all function"
Variance<1, 1, Eigen::Array<double, 1, 1>> specialized;
specialized.f(Eigen::Array<double, 1, 1>{}); // prints "specialization for Eigen::Array<double, rows, cols>"
, , , - .