Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template parameters that depend on other template parameters?

I have found a few similar questions (e.g. this), but none of them really answer mine. Consider this code snippet:

template<unsigned int rows, unsigned int cols,typename arrtype>
class Variance
{
   double f(const arrtype &);
};

template<unsigned int rows, unsigned int cols>
double Variance<rows,cols,Eigen::Matrix<double,rows,cols>>
    ::f(const Eigen::Array<double,rows,cols> & M) 
{
  //do stuff
}

As you can see in the specialization, the type arrtype will depend on rows and cols. The code above results in a compiler error (g++ 5.4.0):

invalid use of incomplete type ‘class Variance<rows, cols, Eigen::Matrix<double, rows, cols> >

I have tried typename arrtype<rows, cols> in the template declaration, but then it complains that arrtype is not a type, which makes sense.

What is the proper way to use templated types that depend on other templated types?

like image 714
Ash Avatar asked Dec 24 '22 08:12

Ash


1 Answers

This is a simplified version of your code:

template<size_t rows, size_t cols> struct Foo {   double foo(); };

template<size_t rows> double Foo<rows,3>::f() { return 3;}

The error you get:

error: invalid use of incomplete type ‘struct Foo<rows, 3ul>’
double Foo<rows,3>::f() { return 3;}

The problem isnt really that one of your template parameters depends on others, but the problem is that you cannot partially specialize a member without partially specializing the class.

This works:

template<size_t rows, size_t cols> struct Foo {   double foo(); };

template<size_t rows> struct Foo<rows,3> { double f() { return 3;}  };
like image 147
463035818_is_not_a_number Avatar answered Jan 11 '23 03:01

463035818_is_not_a_number