non-dependent vs. dependent template names

P

puzzlecracker

See it a lot but haven't learn the difference between this two in the
context of template. Would someone explain it please?

Thanks
 
J

James Kanze

See it a lot but haven't learn the difference between this
two in the context of template. Would someone explain it
please?

It isn't possible to explain it fully in the context of an
article in news; I'd suggest that you get "C++ Templates: the
Complete Guide", by Vandevoore and Josuttis, and study that.
Basically, however: if a name appears in a context that depends
on one of the template arguments (determined by a fairly complex
set of rules), it is considered dependent (unless it is defined
locally, in the template itself). Dependent names are not bound
(the name isn't associated with the entity it refers to) until
instantiation, at which point, the name is looked up twice: once
in the context where the template was defined, and once, using
argument dependent lookup only, in the context where it was
instantiated. The compiler then decides how to bind the name
(or declare the use ambiguous) according to the declarations it
finds from both lookups (again, determined by a fairly complex
set of rules).

The important points to remember is that if you want the name to
be bound at the point of instantiation, you must do something to
make it dependent. In a function call, the name of a function
is dependent if any of its arguments depend on the template
parameters; for other things, and functions without arguments,
you must find some other hack^H^H^H^Hsolution---if a class
template has a dependent base, for example, the use of this->
makes the name to the right of the -> dependent (but limits
lookup to the bases). The other important point to remember is
that if the context in which the name appears isn't dependent,
lookup and binding will occur at the point of definition of the
template, and only at the point of definition. If the name
isn't visible there, it is an error. (What this really means is
that when you get an undefined symbol in a template definition,
the first thing to ask yourself is whether you meant for the
name to be dependent, and if so, find some sort of hack to make
it dependent.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top