Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When are class templates instantiated?

Tags:

c++

Suppose you have the following (ill-formed) program:

struct A
{
    A(int, int)
    {
    }
};

template <typename T>
class B
{
    B()
    {
        if (sizeof (T) == 1)
        {
            throw A(0); // wrong, A() needs two arguments
        }
    }
};

int main()
{
    return 0;
}

GCC compiles this program without any errors, clang++ refuses it with an error.

  1. Is it justified to say thats it is not a bug in GCC because the template isnt instantiated?
  2. What magic does clang do to find this error?
  3. What does the C++ standard say about those situations?
like image 654
cschwan Avatar asked Jan 07 '13 10:01

cschwan


People also ask

When the templates are usually instantiated?

The bodies of template classes and inline (or static) template functions are always instantiated implicitly when their definitions are needed. Member functions of template classes are not instantiated until they are used. Other template items can be instantiated by using explicit instantiation.

Are templates instantiated at compile time?

Templates are instantiated in the process of converting each translated translation unit into an instantiation unit. A translation unit is essentially a source file. A translated translation unit (try to say that three times fast) is the output from compilation without templates instantiated.

Is it necessary to instantiate a template?

In order for any code to appear, a template must be instantiated: the template arguments must be provided so that the compiler can generate an actual class (or function, from a function template).

How are class templates created?

Class Template DeclarationA class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration.


2 Answers

The template is instantiated when used. However, it should be compiled when it's defined. Your code A(0) uses the name A, which doesn't depend on the template parameter T, so it should be resolved when the template is defined. This is called two-phase lookup. The way clang finds the error is simply by trying to resolve the call A(0) as soon as it sees it.

My version of GCC also compiles this code silently, even with -pedantic-errors. Both C++03 and C++11 say that no diagnostic is required, even though the program is ill-formed, so GCC conforms. This is 14.6/7 in C++03 and 14.6/8 in C++11:

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.

like image 147
Steve Jessop Avatar answered Oct 15 '22 10:10

Steve Jessop


  1. Yes. When no valid specialization exist but the template isn't instantiated -- like here -- the program is ill-formed, but no diagnostic is required (14.6/8). So both clang and g++ are right.

  2. clang does more checks than g++ on the template declaration.

  3. See above.

like image 28
AProgrammer Avatar answered Oct 15 '22 09:10

AProgrammer