Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of "if the context from which the specialization is referenced depends on a template parameter"?

According to the C++17 standard, [temp.point]/4, emphasis mine,

For a class template specialization, a class member template specialization, or a specialization for a class member of a class template, if the specialization is implicitly instantiated because it is referenced from within another template specialization, if the context from which the specialization is referenced depends on a template parameter, and if the specialization is not instantiated previous to the instantiation of the enclosing template, 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.

I don't understand how to interpret this sentence. Elsewhere, when describing name lookup in templates, the standard refers to the "instantiation context" and the "definition context". There, the word "context" seems to mean a particular point in the source text, which determines the set of visible names. Here, I'm not sure if the meaning of "context" is the same. If that is what it means, I'm not sure what it means for it to depend on a template parameter. Does that mean it's inside a template, or does it specifically mean there are still some unbound template parameters from enclosing templates at the point where the compiler decides to instantiate the specialization in question, or something else?

Examples would be appreciated.

like image 928
Brian Bi Avatar asked Jun 28 '19 19:06

Brian Bi


1 Answers

This context appears inside an enclosing template (from within another template specialization and depends on a template parameter). It must depends on a template parameter of the enclosing template, so it may be a context inside the definition context of the enclosing template.

So I suppose this paragraph could be illustrated by this example:

template<class T> struct A{};
template<class T> struct B{};

//point of instantiation of B<int>
template<class T> void f(){
   A<T> a;    //The context depends on enclosing template parameter T
   B<int> b;  //The context does not depend on a template parameter.
   }
//
void g(){
  f<double>();
  }
//point of instantiation of A<double>
//point of instantiation of f<double>
like image 198
Oliv Avatar answered Oct 25 '22 11:10

Oliv