2010-11-27 86 views
0

我想传递一个模板模板参数,它的参数是一个非类型值的类型等于以前的模板参数的子类型(哎呀!那是很难说,因为它是阅读!),并且在尝试将结果加入单个参数化模板后出现一些构建错误。只有两个成员参数化后,模板模板参数生成错误

我有以下代码(其编译只是克++ 4.4.1精和-std = C++ 0X):

#include <iostream> 

using namespace std; 

enum class Labels { A , B }; 

template <Labels L> 
struct LabelTypeMap { 
typedef int type_t; 
}; 

template<> struct LabelTypeMap<Labels::B> { typedef double type_t; }; 

template <bool Enable=true> 
struct Hold 
{ 
typedef Labels enum_t; 
}; 

template <> 
struct Hold<true> 
{ 
typedef Labels enum_t; 
}; 

template< typename Holder , template< typename Holder::enum_t > class typeMap , bool Enable > 
struct Whatever 
{ 
template < typename Holder::enum_t label > 
void Operate(typename typeMap<label>::type_t parameter) {}; 
}; 

template< typename Holder , template< typename Holder::enum_t > class typeMap > 
struct Whatever< Holder , typeMap , true > : public Holder 
{ 
template < typename Holder::enum_t label > 
void Operate(typename typeMap<label>::type_t parameter) { cout << "operate on " << parameter << endl; }; 
}; 

template < bool Enable > 
struct Now { 
typedef Hold<true> MyHold; // <----- check this out! 
typedef Whatever< MyHold , LabelTypeMap , Enable > concrete_t; 
}; 

int main() { 
Now<true>::concrete_t obj; 
obj.Operate<Labels::A>(3.222222222222222222222); 
obj.Operate<Labels::B>(3.2222222222222222222); 
}; 

然而,现在,看看Now模板:我有两个构件通过布尔参数进行参数化。 concrete_t取决于封闭Now模板的布尔参数,而MyHold不依赖。我想改变这种状况,所以我代替Now声明与此一:

template < bool Enable > 
struct Now { 
typedef Hold<Enable> MyHold; // <----- boom! 
typedef Whatever< MyHold , LabelTypeMap , Enable > concrete_t; 
}; 

,但是这给了我以下错误:

error: type/value mismatch at argument 2 in template parameter list for ‘template<class Holder, template<typename Holder::enum_t <anonymous> > class typeMap, bool Enable> struct Whatever’ 
error: expected a template of type ‘template<typename Holder::enum_t <anonymous> > class typeMap’, got ‘template<Labels L> struct LabelTypeMap’ 

我一直盯着足够长的时间,在此,我必须说我完全不明白为什么这个简单的改变会引发一个错误。有任何想法吗?

编辑:这里的问题的最小博览会(希望)更容易地思考:

$ cat templatetemplate.cc 
template <int i> 
struct LabelTypeMap { typedef int type_t; }; 

template <bool> 
struct Hold { typedef int type; }; 

template<typename Holder, template<typename Holder::type> class typeMap> 
struct Whatever { }; 

template <bool Enable> 
struct Now { typedef Whatever<Hold<ENABLE>, LabelTypeMap> concrete_t; }; 

Now<true>::concrete_t obj; 

$ g++ -DENABLE=Enable -c templatetemplate.cc 
templatetemplate.cc:11: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Holder, template<typename Holder::type <anonymous> > class typeMap> struct Whatever’ 
templatetemplate.cc:11: error: expected a template of type ↵ 
    ‘template<typename Holder::type <anonymous> > class typeMap’, got ↵ 
    ‘template<int i> struct LabelTypeMap’ 
[email protected]:~/play$ 
$ g++ -DENABLE=true -c templatetemplate.cc 
(no error) 
+0

什么是C++ 0x的连接?啊,发现它:`枚举类`。 – 2010-11-27 22:24:40

+0

我已经删除标签,直到我知道它的实际相关;也许它不是 – lurscher 2010-11-27 22:26:50

回答

1

这里有一个变通,编译,直到有人可以找出一塌糊涂:

template < bool Enable > 
struct Now; 

template <> 
struct Now<false> { 
    typedef Hold<false> MyHold; 
    typedef Whatever< MyHold , LabelTypeMap , false > concrete_t; 
}; 

template <> 
struct Now<true> { 
    typedef Hold<true> MyHold; 
    typedef Whatever< MyHold , LabelTypeMap , true > concrete_t; 
}; 
相关问题