Template Compilation: gcc vs VS2010

From the book - C ++ Templates: A Complete Guide for David, Nicholas

Thus, the templates are compiled twice:

  • Without creating an instance, the template code itself is checked for the correct syntax. Syntax errors, such as missing semicolons, were detected.
  • During instance creation, the template code is checked to ensure that all calls are valid. Invalid calls detected, such as unsupported function calls.

Keeping the first point, I wrote -

template<typename T> void foo( T x) { some illegal text } int main() { return 0; } 

It works great on Visual Studio 2010 without any warnings when optimizations are turned off. Be that as it may, gcc-4.3.4 failed to execute . Which one complies with the C ++ standard? Is it mandatory to compile the template code even if there is no template instance?

+1
c ++ gcc visual-c ++ visual-studio-2010 templates
source share
2 answers

This program is poorly formed, but in this case the C ++ standard does not require diagnostics, therefore both Visual Studio and GCC behave in a compatible way. From paragraph 14.6 / 7 of the C ++ 03 standard (emphasis mine):

Knowing which names are type names, you can check the syntax of each template definition. no diagnostics should be issued to determine the pattern for which a valid specialization can be created. If there is no valid specialization can be generated for the definition of the template, and this template is not created, the template definition is poorly organized, no diagnostics are required. If the type used in the optional name is incomplete at the time the template is defined, but completed at the time the instance is created, and if the completeness of this type affects whether the program is well formed or affects the semantics of the program, the program is poorly formed; no diagnostics required. [Note: if a template is created, errors will be diagnosed in accordance with other rules of this standard. Exactly when these errors are diagnosed is a quality implementation problem. ] [Example:

 int j; template<class T> class X { // ... void f(T t, int i, char* p) { t = i; // diagnosed if X::f is instantiated // and the assignment to t is an error p = i; // may be diagnosed even if X::f is // not instantiated p = j; // may be diagnosed even if X::f is // not instantiated } void g(T t) { +; //may be diagnosed even if X::g is // not instantiated } }; 

-end example]

+7
source share

The book you are looking at seems to reflect (mainly) that the author is watching how the compilers work, and not according to the requirement (standards) of the standard. The standard doesn’t say much about giving the compiler additional leniency about incorrect code inside the template, simply because it has not been created.

At the same time, the book is true that there are some things that the compiler really can’t do much to check until it is created. For example, you can use the dependent name as the name of the function (or call it as a function, one way or another) if it is a functor, and that will be good too) If you instantiate this template above the class where it is a function, fine and good. If you create an instance of a class where it is really int , an attempt to call it will certainly fail. Until you create an instance, the compiler cannot determine what exactly.

This is a big part of what concepts were really intended to be added to C ++. You can directly specify (for example) that the pattern X will call T::y as a function. Then the compiler could compare the contents of the template with the declarations in the concept and determine whether the body of the template matches the declaration in the concept or not. In another direction, the compiler needed to compare a class (or something else) with a concept to determine if an instance of this template would work. If it does not work, it can report the error directly as a violation of the corresponding concept (since now it is trying to create an instance of the template, and you often get some strange error message that poorly indicates the real problem, if at all).

0
source share

All Articles