Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 仅在两个成员都参数化后,才使用模板参数生成错误_C++_Partial Specialization_Template Templates - Fatal编程技术网

C++ 仅在两个成员都参数化后,才使用模板参数生成错误

C++ 仅在两个成员都参数化后,才使用模板参数生成错误,c++,partial-specialization,template-templates,C++,Partial Specialization,Template Templates,我正在尝试传递一个模板参数,其参数是一个非类型值,其类型等于前一个模板参数的子类型(哇!这很难说,因为它很难读取!),在尝试将结果合并到单个参数化模板中后,我遇到了一些生成错误 我有以下代码(使用g++4.4.1和-std=c++0x可以很好地编译): 但这给了我以下错误: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Holder, template<t

我正在尝试传递一个模板参数,其参数是一个非类型值,其类型等于前一个模板参数的子类型(哇!这很难说,因为它很难读取!),在尝试将结果合并到单个参数化模板中后,我遇到了一些生成错误

我有以下代码(使用g++4.4.1和-std=c++0x可以很好地编译):

但这给了我以下错误:

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’
错误:“模板结构”的模板参数列表中参数2的类型/值不匹配
错误:应为“模板类typeMap”类型的模板,但得到了“模板结构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)
$cat templatetemplate.cc
模板
结构标签类型映射{typedef int type_t;};
模板
结构保持{typedef int type;};
模板
结构任意{};
模板
结构现在{typedef Whatever concrete_t;};
现在:具体目标;
$g++-DENABLE=Enable-c templatetemplate.cc
templatetemplate.cc:11:错误:“模板结构”的模板参数列表中参数2的类型/值不匹配
templatetemplate.cc:11:错误:应为类型为的模板↵
“模板类类型映射”,获得↵
“模板结构LabelTypeMap”
marcelo@macbookpro-1:~/play$
$g++-DENABLE=true-c templatetemplate.cc
(没有错误)

在有人能够找出问题的症结之前,这里有一个解决方法:

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;
};
模板
现在构造;
模板
立即构造{
typedef Hold MyHold;
typedef无论什么concrete\t;
};
模板
立即构造{
typedef Hold MyHold;
typedef无论什么concrete\u t;
};

与C++0x的连接是什么?啊,发现了:
enum class
。我已经删除了标记,直到我知道它是否真的相关;可能不是这样(至少对于
enum类
,尽管这不是问题的重点)如果我将
enum类
更改回一个好的旧
enum
,我会看到同样的问题。我不是语言专家,但我怀疑gcc有漏洞。我认为如果
无论什么
的第二个模板参数
模板类typeMap
应该与
模板结构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)
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;
};