Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Point of instantiation of a template class

Could that code to be compile?

#include <iostream>

template <typename T>
struct TMPL
{
    using TP = typename T::TP; //is CL::TP visible (with T == CL)?
};

struct CL
{
    using TP = int;
    TMPL<CL>::TP val; 
};

int main()
{
    CL cl;
}

TMPL is instantiated immediately before CL class definition according to Standard 14.6.4.1/4

For a class template specialization, ..., if the specialization is implicitly instantiated because it is referenced from within another template specialization, .... Otherwise, the point of instantiation for such a specialization immediately precedes the namespace scope declaration or definition that refers to the specialization.

So, CL::TP isn't visible in TMPL instantiation point, but all the compilers (MSVC, gcc, clang) compile it fine. I also has found a defect report http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#287, but it, apparently, wasn't accepted

like image 586
Denis Avatar asked Jun 10 '16 07:06

Denis


People also ask

What is the instantiation of the class template?

The act of creating a new definition of a function, class, or member of a class from a template declaration and one or more template arguments is called template instantiation.

What is the point of instantiation?

Whenever a template specialization is referenced in a context that requires instantiation, that context gives birth to a "point of instantiation" (which effectively denotes a location where the compiler is allowed to generate code for the referenced template specialization).

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.

What is instantiation in C++?

In C++, the creation of a new instance of the class is called instantiation. Memory is allocated for that object and the class constructor runs. Programmers can instantiate objects on the heap with a new keyword or on the stack as a variable declaration.


1 Answers

Your example is not identical to the one in the defect report. In the defect report, CL is a class template. However the intent of the proposed resolution is to make the template case the same as the non-template one, aka [basic.scope.pdecl]:

6 After the point of declaration of a class member, the member name can be looked up in the scope of its class. [ Note: this is true even if the class is an incomplete class. For example,

struct X {
  enum E { z = 16 };
  int b[X::z];      // OK
};

end note ]

Then the proposed resolution:

In 14.6.4.1 [temp.point] paragraph 3 change:

the point of instantiation is immediately before the point of instantiation of the enclosing template. Otherwise, the point of instantiation for such a specialization immediately precedes the namespace scope declaration or definition that refers to the specialization.

To:

the point of instantiation is the same as the point of instantiation of the enclosing template. Otherwise, the point of instantiation for such a specialization immediately precedes the nearest enclosing declaration. [Note: The point of instantiation is still at namespace scope but any declarations preceding the point of instantiation, even if not at namespace scope, are considered to have been seen.]

Add following paragraph 3:

If an implicitly instantiated class template specialization, class member specialization, or specialization of a class template references a class, class template specialization, class member specialization, or specialization of a class template containing a specialization reference that directly or indirectly caused the instantiation, the requirements of completeness and ordering of the class reference are applied in the context of the specialization reference.

As of the latest draft, the non-template case was and is still valid. The template case is not. However the defect is drafting, which means that the template case is intended to compile.

Drafting: Informal consensus has been reached in the working group and is described in rough terms in a Tentative Resolution, although precise wording for the change is not yet available.

like image 73
uh oh somebody needs a pupper Avatar answered Oct 24 '22 10:10

uh oh somebody needs a pupper