c++ template of template
#include "boost/numeric/ublas/matrix.hpp"
using namespace boost::numeric::ublas;
template <class matrixform,class input_T>
class Layer
{
private:
matrixform <input_T>;
public:
};
I want to be able to do
int main ()
{
Layer<identity_matrix, double> mylayer;
}
BUT
layer.hpp:18: error: ‘matrixform’ is 开发者_运维问答 not a template layer.hpp:18: error: declaration does not declare anything
You would need to use a template template parameter:
template <template <class> class matrixform, class input_T>
class Layer { /* ... */ };
Note that in order to use a template template parameter, the template provided as an argument must have parameters that exactly match the list. So, in this example here, only a template taking one type parameter can be used to instantiate Layer
.
This is a problem, especially since a class template parameter can have a default argument. The Boost identity_matrix
class, for example, has two template parameters: the type and the allocator to be used, so it could not be used to instantiate Layer
(the allocator parameter has a default argument).
What you can do instead is take as a template parameter the instantiated template to use, and get the input_T
from a typedef defined by that type. For example:
template <typename MatrixT>
class Layer
{
typedef typename MatrixT::value_type ValueT;
};
MatrixT
here is what matrixform<input_T>
is in your example, and ValueT
is your input_T
.
This can be instantiated as,
Layer<identity_matrix<double> > mylayer;
template <class templatedmatrixform>
class Layer
{
private:
templatedmatrixform matrix;
public:
};
template<typename T>
class identity_matrix
{
typedef T input_T;
};
int main ()
{
Layer<identity_matrix<double> > mylayer;
}
精彩评论