This is the statement from ISO C++ Standard 14.6.4.1 Point of instantiation
4.If a virtual function is implicitly instantiated, its point of instantiation
is immediately following the point of instantiation of its enclosing
class template specialization.
5.An explicit instantiation directive is an instantiation point for the
specialization or specializations specified by the explicit
instantiation directive.
6.The instantiation context of an expression that depends on the
template arguments is the set of declarations with external linkage
declared prior to the point of instantiation of the template
specialization in the same translation unit.
I am unable to write a programs for this whole section. I am trying to write a programs for this section from yesterday.
Please, normally I would try to ask a 1 or more points. In any section. But here I am unable to understand a single point in this section.
So, kindly can any one provide me a code for this sections to understand.
The first two statements explain where the instantiation point of certain template constructs are; it doesn't introduce new template constructs. So you can reuse your previous examples.
The third statement (14.6.4.1/6) tells us what the point of instantiation points is: they are the point where names are looked up during the second phase of name lookup. Names that are declared before the instantiation point are visible; those declared afterwards are not. (In the first phase of two-phase name lookup, non-dependent names are looked up in the set of declarations that precede the template definition).
So, given:
the instantiation contexts of the expression
T()+T()
is the set of declarations that precede the respective instantiation points offoo<T>
. The nameoperator+
is looked up in those contexts, and includes declarations that follow this definition but precede the instantiation point.There seems always tons of questions with regards to instantiation context.
The example given by MSalters is problematic:
consider the following code:
That compiles and runs on all compilers, but if you put the class A definition into a namespace:
Clang will fail to compile, but VC++ and gcc compiles. Why? which compiler conforms to the spec?
Frankly, I don't know. Some compiler, like gcc even contradicts itself in this area. Consider the following code:
Simply change from "operator+" to a function named "g", gcc fails to compile???Why???
If the Spec is correct, then why GCC cannot find 'g'?
When I was reading Bjarne Stroustrup's "The C++ Programming Language, 4th Edition", 26.3.5 Template and Namespaces, he has this example:
Here, f(t) is clearly dependent, so we cannot bind f at the point of definition. To generate a specialization for g(N::A), the compiler looks in namespace N for functions called f() and fins N::f(N::A).
The f(int) is found because it is in scope at the point of definition of the template. The f(double) is not found because it is not in scope at the point of definition of the template, and argument-dependent lookup does not find a global function takes only arguments of built-in types.
So it's a mess!