Forward declaration of class used in template function is not compiled by clang++
c++, clang++, forward-declaration, g++, templates
Solution
Which compiler is right then according to C++ standard?
Both are correct. This is an ill-formed program. Emphasis mine:
N3290 14.6¶9 If a type used in a non-dependent name is incomplete at the point at which a template is defined but is complete at the point at which an instantiation is done, and if the completeness of that type affects whether or not the program is well-formed or affects the semantics of the program, the program is ill-formed; no diagnostic is required.
That clang++ and other compilers do issue a diagnostic here is a nice-to-have added feature, but a diagnosis is not mandatory. That clause "the program is ill-formed; no diagnostic is required" gives a compiler developer free reign to do just about anything in such circumstances and still be compliant.
Problem
There is this code: ``` class A; template <class T> void fun() { A a; } class A { public: A() { } }; int main() { fun<int>(); return 0; } ``` g++ 4.5 and g++ 4.7 compiles this without error. But clang++ 3.2 (trunk) gives this error: ``` main.cpp:5:6: error: variable has incomplete type 'A' A a; ^ main.cpp:1:7: note: forward declaration of 'A' class A; ^ ``` Which compiler is right then according to C++ standard?