C ++ CRTP (templated template) question

the following code snippet doesn't compile, the problem is T::rank

not inaccessible (I think) or uninitialized in the parent template.

Can you tell me what the problem is? explicitly skips rank the only way? or is there a way to directly query the tensor class?

thanks

#include <boost/utility/enable_if.hpp>

template<class T, // size_t N,
         class enable = void>
struct tensor_operator;

// template<class T, size_t N>
template<class T>
struct tensor_operator<T, typename boost::enable_if_c< T::rank == 4>::type > {
    tensor_operator(T &tensor) : tensor_(tensor) {}
    T& operator()(int i,int j,int k,int l) {
        return tensor_.layout.element_at(i, j, k, l);
    }
    T &tensor_;
};

template<size_t N, typename T = double>
// struct tensor : tensor_operator<tensor<N,T>, N> {
struct tensor : tensor_operator<tensor<N,T> > {
    static const size_t rank = N;
};

 tensor <4> D;  // compiler attempts to instantiate undefined template, not specialization

      

I know a workaround, however I am interested in the mechanics of creating a self-education template

+2


a source to share


2 answers


In CRTP, the base class template takes advantage of the fact that function members (definitions) are not instantiated until their declarations are declared. In your code, the base class depends on the incomplete type.



+2


a source


Am I the only one looking at infinite recursion here?

  • tensor<N,T>

    depends on tensor_operator< tensor<N,T> >

  • tensor_operator< tensor<N,T> >

    depends on tensor<N,T>

I don't remember a situation where I used a property of a class Derived

to decide whether to instantiate or not Base

, but it seems to me that it might cause infinite recursion.



Here is the bug on gcc 3.4.2:

 In instantiation of `tensor<4ul, double>':
41:   instantiated from here
33: error: invalid use of undefined type
                          `struct tensor_operator<tensor<4ul, double>, void>'
19: error: declaration of `struct tensor_operator<tensor<4ul, double>, void>'

      

The problem here is that creation tensor_operator<N,T>

is instance dependent tensor_operator<N,T>

...

+2


a source







All Articles