下面的代码可以用 clang 编译,但不能用GCC编译(试过 4.1.2、4.5.4 和 4.7.2):
template <typename T>
struct A
{
struct B { };
};
template <typename T>
bool operator==(typename A<T>::B const& b, T const& t);
enum { BAR };
template <typename T>
bool test()
{
return 0 == BAR;
}
来自 GCC 4.7.2 的错误消息是:
a.cpp: In instantiation of ‘struct A<<anonymous enum> >’:
a.cpp:12:6: required by substitution of ‘template<class T> bool operator==(const typename A<T>::B&, const T&) [with T = <anonymous enum>]’
a.cpp:19:17: required from here
a.cpp:6:12: error: ‘<anonymous enum>’ is/uses anonymous type
a.cpp:6:12: error: trying to instantiate ‘template<class T> struct A<T>::B’
a.cpp:6:12: error: ‘<anonymous enum>’ is/uses anonymous type
a.cpp:6:12: error: trying to instantiate ‘template<class T> struct A<T>::B’
GCC拒绝代码是正确的,还是我遇到了它的错误?
PS 我在尝试构建一个开源项目时看到了这个错误。我试图制作尽可能小的例子来重现它。