Patterns: Name Resolution: Point of Instance: & # 8594; can anyone tell some more examples for this statement?

This statement is from ISO C ++ 14.6.4.1. Instance Point

  • To specialize a function template, the template of a member function of a specialization or specialization for a member function or a static data member of a class template, if the specialization is an implicit instance, because it refers from another template, the specialization and the context with which it refers depend on the template parameter, the point of creation of the specialization is the instantiation point of encompassing specialization. Otherwise, the instantiation point for such a specialization immediately follows the declaration or definition of the namespace region that refers to the specialization.

  • If a function template or member function of a class template is invoked in a way that uses the definition of the default argument of this function as a template or member function, the default instance instantiation point is the creation point of the function template or member function specialization.

  • For a template template specialization, a specialization of a member template, or a specialization for a member of a class template class class, if the specialization is implicitly created because it is referenced in another specialized specialization, if the context from which the reference to the specialization depends on the template parameter, and if the specialization is not created Prior to instantiating the spanning template, the instantiation point occurs immediately before the template creating point. Otherwise, the point at which such a specialization is created immediately precedes the declaration of the namespace area or the definition that relates to the specialization.

I can not write programs for this entire section. I have been trying to write programs for this section since yesterday.

Can someone provide me with code to understand these sections.

Please, usually .. I tried to set 1 or more points. In any section. But here I can not understand one point in this section.

So, kindly can someone provide me with code (programs) for understanding these sections.

+4
source share
2 answers

I find it perfectly reasonable, and the committee has more of that pleasure . Therefore, I think that I probably have some errors in the below. So please read it with care :)

Third paragraph

For specialization of a class template, specialization of a class member template, or specialization for a class member of a class template, if the specialization is implicitly created because it is referenced from another template specialization if the context from which the specialization refers to the template parameter and if the specialization is not created before creation instance of the closing template, the instantiation point is immediately in front of the instantiation point of the covering template.

In other words, if an instance is created of a class template or a nested class of a class template, and the context that triggers this event depends on the template parameter, the template / nested class is created immediately before the instantiation point of the template that references it.

The context in another specialization can either depend on the template parameters, as for primary templates, partial specializations and members of the class template, or it does not depend on the template parameters, which is the case for explicit references from within.

Otherwise [i.e. context did not depend], the instance point for such a specialization immediately precedes the declaration or definition of the namespace area that relates to the specialization.

This distinction is important. Think about what happens if the instantiation point for specializations from dependent contexts immediately precedes the declaration of the namespace scope or the definition that refers to it

template<typename T, int N> struct A { typedef typename A<T, N-1>::type *type; }; template<typename T> struct A<T, 0> { typedef T type; }; typedef A<int, 2>::type ptr; 

This template should add N pointer declarators. So A<int, 2> is int** , for example.

  • The context around typedef A<int, 2>::type is independent, so A<int, 2> is created before the typedef declaration.
  • In A<int, 2> we have A<int, N-1>::type , which appears in a dependent context and which refers to A<int, 1>::type . Therefore, the Standard requires that we create an instance of A<int, 1> at the same point at which we created an instance of A<int, 2> .

    If we imagine this immediately before declaring the namespace scope that referred to it (before defining the primary template), we would not notice a partial specialization of A<T, 0> when processing `A<int, N-1>::type inside A<int, 1> because we will instantiate A<int, 1> before this specialization.

Second paragraph

It’s just that the names found in the default arguments are consistent with the names that were looked at in the rest of the function for which they are used (i.e. their POI matches the POI of their template function / member function of the template class) .

First paragraph

This is basically the same as in the third paragraph. However, function templates are created after the object that references them so that recursive use can be used, as in the following example. In contrast, class templates are created in front of an entity that references them because the object requires the class type to be completed. If the class type of the POI is after this object, the class type will still be absent.

 template<typename T> void f(T); template<typename T> struct A { void g() { f(0); } void h() { /* do a dance */ } }; template<typename T> void f(T t) { A<T> a; ah(); } void g() { A<int> a; ag(); } 

If f is created before A<int> , then it will not be able to access ah() , because at that moment it did not exist. Therefore, function templates are created after the object that refers to them, and class templates are created in front of the entity that refers to them.

+5
source

Ask someone to correct my understanding ...

I think the code below illustrates what 1 and 2 mean (from what I understood):

 template<class T> void f1(T t){cout << 0;} template<class T> void f2(T t, void (*p)(int) = f1){ (*p)(0); } void f(double d){cout << 1;} template<class T> void g(T t){ f1(t); // this is the interesting call. } void f1(int t){cout << 2;} int main(){ g(2); f2(2); } // POI for f1(t), Also POI for f2(2) 

Consider the call to g (2). There are three types of congestion (viable) in the POI that are visible:

  • function template
  • e (intermediate)
  • F (twice).

However, the call refers to 'f (int)', as this is the best match.

Similarly, the POI for 'f2 (2)' is the closing parenthesis of main. The argument "f1" is again checked by default with this POI and resolved to "f1 (int)", which is the best match for all three available overloads.

Thanks @litb. Revised after @litb fixed my understanding:

 double f1(double d){cout << 1; return 0.0;} template<class T> void f2(T t1, T t2 = f1(T())){} template<class T> void g(T t){ f1(t); // this is the interesting call. } struct A{ friend A f1(A const &){cout << 2; return A();} }; int main(){ g(A()); // 'f1(t)' resolves to friend function declaration f2(A()); // The call to 'f1' in default argument also resolves to friend // function declaration // This is because for non dependent names, only ADL is performed // at POI which is at closing brace of main. } // POI for f1(t), Also POI for f2(2) in that order 

Remember that in both of the above calls there are two overloads that are candidates. The namspace function "f1 (double)" and the function "f1" of the friend function (found due to ADL). Since this is the only viable function, calls allow the declaration of the friend 'f1'.

I think paragraph 3 means this:

 template<class A> struct T{ T(int x = 0){} }; template<class A> struct U{ U():t(f(A())){} T<A> t; }; // POI of 'gt' instantiation T<int> gt(f(2)); // At this point 'f' is not found, hence error int f(int x){return x;} int main(){ U<int> u; } // There are 2 POIs here 'U<int>::T<int>' and 'U<int>' and in that order // Therefore 'f' is visible from here. 
0
source

All Articles