tags:

views:

86

answers:

2

This is the statement from ISO C++ Standard 14.6.4.1 Point of instantiation

  1. For a function template specialization, a member function template specialization, or a specialization for a member function or static data member of a class template, if the specialization is implicitly instantiated because it is referenced from within another template specialization and the context from which it is referenced depends on a template parameter, the point of instantiation of the specialization is the point of instantiation of the enclosing specialization. Otherwise, the point of instantiation for such a specialization immediately follows the namespace scope declaration or definition that refers to the specialization.

  2. If a function template or member function of a class template is called in a way which uses the definition of a default argument of that function template or member function, the point of instantiation of the default argument is the point of instantiation of the function template or member function specialization.

  3. 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 am unable to write a programs for this whole section. I am trying to write a programs for this section from yesterday.

Can any one provide me a code for this sections to understand.

Please, normally ..I tried to ask a 1 or more points. In any section. But here iam unable to understand a single point in this section.

So, kindly can any one provide me a code(programs) for this sections to understand.

+1  A: 
Chubsdad
Well, "At the POI, there are basically three overloads (viable) that are visible" -> at the POI, only ADL is done (14.6.4/2). Because the ADL set for int is empty, only functions from the definition context are considered. So it will choose the template `f1`. For your default argument example, `f1` is a non-dependent name, so it can only find the template `f1` too (see 14.7.1/11).
Johannes Schaub - litb
@Johannes Schaub - litb: Many thanks. Guess it is fine now.
Chubsdad
+2  A: 

I find this quite mind-screwing, and the committee has more such fun. So I think it's likely I have some errors in the below. So please read it with care :)

Third paragraph

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.

In other words, if a class template or a nested class of a class template is instantiated, and the context that causes that instantiation depends on a template parameter, the template/nested class is instantiated immediately before the point of instantiation of the template that refers to it.

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

Otherwise [i.e. the context is nondependent], the point of instantiation for such a specialization immediately precedes the namespace scope declaration or definition that refers to the specialization.

This distinction is important. Consider what would happen if the point of instantiation for specializations from dependent contexts would preceede immediately to the namespace scope declaration or 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 is supposed to add N pointer declarators. So A<int, 2> is int** for example.

  • The context around typedef A<int, 2>::type is non-dependent, so A<int, 2> is instantiated before the typedef declaration.
  • Within A<int, 2>, we have A<int, N-1>::type, which appears in a dependent context and which references A<int, 1>::type. So the Standard requires us to instantiate A<int, 1> at the same point we instantiated A<int, 2>.

    If we would instantiate this immediately before the namespace scope declaration that referred to it (before the primary template definition), we would not notice the partial specialization A<T, 0> when processing `A<int, N-1>::type within A<int, 1> because we would instantiate A<int, 1> before that specialization.

Second paragraph

This is just so that names looked up in default arguments are consistent with names looked up in the rest of the function that they are used for (i.e their POI is the same as the POI of their function template / member function of class template).

First paragraph

This is basically the same as the third paragraph. However, function templates are instantiated after the entity that refer to them so that recursive uses are possible, like in the following example. In contrast, class templates are instantiated before the entity that refer to them because the entity required that class type to be complete. If the class type's POI would be after that entity, the class type would still be non-existent.

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;
  a.h();
}

void g() {
  A<int> a; 
  a.g();
}

If f would be instantiated before A<int>, then it could not access a.h() because at that point it did not exist yet. Therefor, function templates are instantiated after the entity that refer to them, and class templates are instantiated before the entity that refer to them.

Johannes Schaub - litb
@Johannes Schaub - litb: Thank You Very much.
BE Student