g ++ - 4.1模糊类模板实例化变通方法?

时间:2012-09-25 03:24:55

标签: 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, 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

0 个答案:

没有答案