non-dependent vs. dependent template names

Discussion in 'C++' started by puzzlecracker, Aug 6, 2008.

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

    puzzlecracker, Aug 6, 2008
    1. Advertisements

  2. James Kanze

    James Kanze Guest

    On Aug 6, 11:04 pm, 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?

    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:
    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
    James Kanze, Aug 7, 2008
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Evan
  2. er
  3. George2

    non-dependent name in template function

    George2, Mar 10, 2008, in forum: C Programming
    Mar 10, 2008
  4. nguillot
  5. Replies:
    Zhihao Yuan
    Nov 20, 2012