The code below compiles fine with clang, but does not compile with GCC (tried 4.1.2, 4.5.4 and 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;
}
The error message from GCC 4.7.2 is:
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’
Is GCC correct in rejecting the code, or am I hitting its bug?
P.S. I've seen this error while trying to build one of opensource projects. I tried to make smallest possible example that reproduces it.
main
and template functions are only instantiated when called, so if you're not calling any function, this a.cpp should definitely not compile, nor do anything. – Abbatial-c
option to just compile without linking). An empty file will compile successfully. – Existenceg++ a.cpp -c -std=c++0x
compiles fine. The bug seems to be fixed in the c++11 specific parts of g++. – Ipswich0 == BAR
should be compiled during phase one, no instantiation needed. – Ingleside