Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the compilation of this code standard behaviour?

Tags:

c++

templates

Consider the following code:

struct Undefined;

template <typename T>
void TemplateFunction()
{
  Undefined obj;
}

int main()
{
  return 0;
}

I was always under the impression that template functions and template methods (or methods part of a template class) will only be checked for syntax (unless they are instantiated) in which case the above code should compile.

Problem is, it does not compile with Xcode (ver: 4.3.2, using the LLVM compiler) and now I am wondering whether the above code is non-standard i.e. it should not compile on a compiler conforming to the C++03 standard? The error from Xcode is:

Variable has incomplete type "Undefined"

Note that in the original code, the undefined object is part of the static assertion.

like image 863
Samaursa Avatar asked Aug 12 '12 00:08

Samaursa


1 Answers

The compiler is right in rejecting the code, even though other compilers will gladly accept it. In particular the quote would be within §14.6[temp.res]/8

[...] If no valid specialization can be generated for a template definition, and that template is not instantiated, the template definition is ill-formed, no diagnostic required. [...] 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; [...]

That is, the template is ill-formed although the compiler is not required to diagnose it.

like image 89
David Rodríguez - dribeas Avatar answered Sep 16 '22 20:09

David Rodríguez - dribeas