以下一段代码无法编译,问题出现在T::rank
不能无法访问(我认为)或在父模板中未初始化。
你能告诉我究竟是什么问题吗? 明确通过排名唯一的方法?或者有没有办法直接查询张量类?
谢谢
#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
我知道解决方法,但我对自我教育模板实例化的机制感兴趣
答案 0 :(得分:2)
我是唯一一个在这里看无限递归的人吗?
tensor<N,T>
取决于tensor_operator< tensor<N,T> >
tensor_operator< tensor<N,T> >
取决于tensor<N,T>
我不记得我使用Derived
类属性来决定是否实例化Base
的情况,但在我看来这样会导致无限递归发生。< / p>
以下是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>'
这里的问题似乎是tensor_operator<N,T>
的实例化取决于tensor_operator<N,T>
的实例化...
答案 1 :(得分:2)
在CRTP中,基类模板利用了成员函数体(定义)在声明之后很久才实例化的事实。在您的代码中,基类依赖于不完整的类型。