此代码被其他编译器接受,但失败并显示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;
}
编译器输出:
$ 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
的默认模板参数的存在bar
似乎是罪魁祸首。
有没有其他方法可以foo
为这个编译器实现?