Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

non-dependent vs. dependent template names

5 views
Skip to first unread message

puzzlecracker

unread,
Aug 6, 2008, 5:04:30 PM8/6/08
to
See it a lot but haven't learn the difference between this two in the
context of template. Would someone explain it please?

Thanks

Victor Bazarov

unread,
Aug 6, 2008, 6:14:44 PM8/6/08
to
puzzlecracker wrote:
> See it a lot but haven't learn the difference between this two in the
> context of template. Would someone explain it please?

Would an example suffice?

template<class T> struct BT { int dependent; };

struct B { int nondependent; };

template<class T> struct D1 : BT<T> {
void foo() { dependent = 42; } // error!!!!
void bar() { this->dependent = 42; } // OK
};

template<class T> struct D2 : B {
void foo() { nondependent = 42; } // OK
void bar() { this->nondependent = 42; } // OK
};

int main() {
D1<int> d1;
D2<int> d2;
}

BTW, have you read the FAQ?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask

James Kanze

unread,
Aug 7, 2008, 3:42:53 AM8/7/08
to

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.)

--
James Kanze (GABI Software) email:james...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

0 new messages