下面這段代碼不能編譯,問題出在T::rank
以上,在父模板中不能無法訪問(我認爲)或未初始化。C++ CRTP(模板模式)問題
你能告訴我到底是什麼問題嗎? 明確通過排名唯一的方式?或者有沒有辦法直接查詢張量類?
謝謝
#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
我知道了解決方法,但我感興趣的模板實例化的機制進行自我教育
它可能與我在visual studio中發現的這個錯誤有關嗎? http://connect.microsoft.com/VisualStudio/feedback/details/354162/seemingly-valid-dependant-types-in-nested-template-not-accepted – 2010-05-19 05:01:02
@kirill g ++ 4.3。我忘了添加實例化的最後一行,現在修復 – Anycorn 2010-05-19 05:01:19
@evan它看起來確實相似 – Anycorn 2010-05-19 05:04:27