问题描述
我试图传递一个模板模板参数,其参数是类型的非类型值等于以前的模板参数的子类型(这是很难说,因为它是阅读!)和我尝试在单个参数化模板中加入结果后,我有一些构建错误。
我有以下代码(编译刚好与g ++ 4.4.1和-std = c ++ 0x):
#include< iostream>
using namespace std;
枚举类标签{A,B};
template< Labels L>
struct LabelTypeMap {
typedef int type_t;
};
模板<> struct LabelTypeMap< Labels :: B> {typedef double type_t; };
template< bool Enable = true>
struct Hold
{
typedef标签enum_t;
};
模板<>
struct Hold< true>
{
typedef标签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< 操作在<参数<< endl; };
};
template< bool启用>
struct Now {
typedef Hold< true>我的//< ----- 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);
};但是,现在,看看现在 取决于封装的现在模板的bool参数,而 MyHold 不。我想改变,所以我用这一个替换现在声明:template< bool启用>
struct Now {
typedef Hold< Enable>我的//< ----- boom!
typedef Whatever< MyHold,LabelTypeMap,Enable> concrete_t;
};
但这会给我以下错误:
错误:模板参数列表中参数2的类型/值不匹配'template< class Holder,template< typename Holder :: enum_t< anonymous> > class typeMap,bool Enable> struct Whatever'
错误:预期的模板类型'模板< 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; };
现在< true> :: concrete_t obj;
$ g ++ -DENABLE =启用-c templatetemplate.cc
templatetemplate.cc:11:错误:模板参数列表中的第2个参数的类型/值不匹配'template< class Holder,template< ; typename Holder :: type< anonymous> > class typeMap> struct Whatever'
templatetemplate.cc:11:error:expected一个类型为↵的模板
'template< typename Holder :: type< anonymous> > class typeMap',got↵
'template< int i> struct LabelTypeMap'
marcelo @ macbookpro-1:〜/ play $
$ g ++ -DENABLE = true -c templatetemplate.cc
(无错误)
解决方案这里有一个编译工作,直到有人能弄明白为止:
template< bool启用>
struct Now;
模板<>
struct Now< false> {
typedef Hold< false>我的
typedef Whatever< MyHold,LabelTypeMap,false> concrete_t;
};
模板<>
struct Now< true> {
typedef Hold< true>我的
typedef Whatever< MyHold,LabelTypeMap,true> concrete_t;
};
I am trying to pass a template template parameter whom its parameter is a non-type value of type equal to a subtype of a previous template parameter (whew! that was as hard to say as it is to read!), and i'm having some build errors after trying to join the results in a single parametrized templates.
I have the following code (which compiles just fine with g++ 4.4.1 and -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 ); };However, Now, look at the Now template: I have two members that are parametrized by booleans. concrete_t depends however on the bool parameter of the enclosing Now template, while MyHold does not. I want to change that, so i replace the Now declaration with this one:
template < bool Enable > struct Now { typedef Hold<Enable> MyHold; // <----- boom! typedef Whatever< MyHold , LabelTypeMap , Enable > concrete_t; };but this gives me the following errors:
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’I've stared long enough at this and i must say that i completely fail to see why this simple change would trigger an error. any ideas?
EDIT: Here's a minimal exposition of the problem to (hopefully) make it easier to ponder:
$ 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)解决方案Here's a work-around that compiles, until someone can figure out the mess:
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; };
这篇关于仅当两个成员都进行参数化后,才会使用模板模板参数生成错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!