0

我正在尝试传递一个模板模板参数,它的参数是一个非类型值,其类型等于先前模板参数的子类型(哇!这很难说,因为它很难读!),我是尝试将结果加入单个参数化模板后出现一些构建错误。

我有以下代码(使用 g++ 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模板的 bool 参数,而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’
marcelo@macbookpro-1:~/play$ 
$ g++ -DENABLE=true -c templatetemplate.cc
(no error)
4

1 回答 1

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;
};
于 2010-11-27T23:19:42.823 回答