Thanks CornedBee,
I think if you mean inside the global sort function, it can not call member function sort of Container class, it is nonsenses, since it breaks the basic C++ rule -- how could you invoke a member function of the class but not through class instance name/pointer name/class name?
Any comments and what do you think are Bjarne's points?
As a reference I post all the content of the chapter here.
C.13.8.3 Point of Instantiation Binding
Each use of a template for a given set of template arguments defines a point of instantiation. That
point is in the nearest global or namespace scope enclosing its use, just before the declaration that
contains that use. For example:
Code:
t e m p l a t e <c l a s s T > v o i d f (T a ) { g (a ); }
v o i d g (i n t );
v o i d h ()
{
e x t e r n g (d o u b l e );
f(2) ;
}
Here, the point of instantiation for f <i n t >() is just before h (), so the g () called in f () is the global
g (i n t ) rather than the local g (d o u b l e ). The definition of ‘‘instantiation point’’ implies that a
template parameter can never be bound to a local name or a class member. For example:
Code:
v o i d f ()
{
s t r u c t X { /* ... */ }; // local structure
v e c t o r <X > v ; // error: cannot use local structure as template parameter
/ / ...
}
Nor can an unqualified name used in a template ever be bound to a local name. Finally, even if a
template is first used within a class, unqualified names used in the template will not be bound to
members of that class. Ignoring local names is essential to prevent a lot of nasty macrolike
behavior.
For example:
Code:
t e m p l a t e <c l a s s T > v o i d s o r t (v e c t o r <T >& v )
{
s o r t (v .b e g i n (), v .e n d ()); // use standard library sort()
}
c l a s s C o n t a i n e r {
v e c t o r <i n t > v ; // elements
/ / ...
p u b l i c :
v o i d s o r t () // sort elements
{
s o r t (v ); // invokes sort(vectorint&) rather than Container::sort()
}
/ / ...
};
If the point of instantiation for a template defined in a namespace is in another namespace, names
from both namespaces are available for name binding. As always, overload resolution is used to
choose between names from different namespaces (§8.2.9.2).
Note that a template used several times with the same set of template arguments has several
points of instantiation. If the bindings of independent names differ, the program is illegal. However,
this is a difficult error for an implementation to detect, especially if the points of instantiation
are in different translation units. It is best to avoid subtleties in name binding by minimizing the
use of nonlocal names in templates and by using header files to keep use contexts consistent.
Originally Posted by
CornedBee
1)
It doesn't. But were the PoI rules different, it might. That's the whole point of Bjarne's comment.
regards,
George