C++ g++-4.1模棱两可的类模板实例化解决方法?
此代码为其他编译器所接受,但因C++ g++-4.1模棱两可的类模板实例化解决方法?,c++,templates,template-meta-programming,C++,Templates,Template Meta Programming,此代码为其他编译器所接受,但因g++-4.1而失败: template<typename T> struct foo; template<template<typename> class ClassTemplate, typename Arg> struct foo<ClassTemplate<Arg> > { typedef Arg type; }; template<template<typename, ty
g++-4.1
而失败:
template<typename T> struct foo;
template<template<typename> class ClassTemplate, typename Arg>
struct foo<ClassTemplate<Arg> >
{
typedef Arg type;
};
template<template<typename, typename> class ClassTemplate, typename Arg1, typename Arg2>
struct foo<ClassTemplate<Arg1,Arg2> >
{
typedef Arg1 type;
};
template<typename T1, typename T2 = int> class bar {};
int main()
{
typedef bar<int> test_me;
typedef foo<test_me>::type type;
return 0;
}
模板结构foo;
模板
结构foo
{
typedef-Arg型;
};
模板
结构foo
{
typedef Arg1型;
};
模板类条{};
int main()
{
typedef-bar-test\u-me;
typedef-foo::type-type;
返回0;
}
编译器输出:
$ g++ test.cpp
test.cpp: In function ‘int main()’:
test.cpp:20: error: ambiguous class template instantiation for ‘struct foo<bar<int, int> >’
test.cpp:5: error: candidates are: struct foo<ClassTemplate<Arg> >
test.cpp:11: error: struct foo<ClassTemplate<Arg1, Arg2> >
test.cpp:20: error: ‘type’ in class ‘foo<bar<int, int> >’ does not name a type
$g++test.cpp
test.cpp:在函数“int main()”中:
test.cpp:20:错误:“struct foo”的类模板实例化不明确
test.cpp:5:错误:候选项为:struct foo
test.cpp:11:错误:struct foo
test.cpp:20:错误:“foo”类中的“type”未命名类型
bar
的默认模板参数的存在似乎是罪魁祸首
有没有其他方法可以为这个编译器实现
foo
?在typedef foo::type中不应该有typename
代码>?像typedef-typename-foo::type-type
.No,typename
不能在模板之外使用,并且main
不是模板。在模板定义中,typename可以用于声明依赖名称是类型。看。或者更好的是。