gpt4 book ai didi

c++ - 仅在两个成员都参数化后使用模板模板参数构建错误

转载 作者:行者123 更新时间:2023-11-28 08:25:12 26 4
gpt4 key购买 nike

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

我有以下代码(使用 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 模板:我有两个由 bool 值参数化的成员。然而,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)

最佳答案

这是一个可以编译的变通方法,直到有人能弄清楚问题为止:

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;
};

关于c++ - 仅在两个成员都参数化后使用模板模板参数构建错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4294180/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com