tags:

views:

66

answers:

2
#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

+2  A: 

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;
James McNellis
+1, you were 10 seconds faster. :)
Prasoon Saurav
can you plz explain what's inside the class ?
ismail marmoush
@ismail: What, exactly, do you not understand? (And, why is it that typing "please" is so difficult?)
James McNellis
sry man ddn mean 2 mk u angry :) , p.s i replied before you edit :) so i didn't see your full answer
ismail marmoush
A: 
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; 
} 
James Curran
identity_matrix is already defined in boost , with bunch of others like triangular_matrix so ?
ismail marmoush
I didn't realize to was a pre-defined class. The typedef is there just incase you need that type elsewhere in Layer. Fortunately, the boost class defines it as `value_type`, so whereever you'd use `input_T` in your Layer class, you can use `templatedmatrixform::value_type`.
James Curran