Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the definition of "dependent name" in C++?

In C++, the concept of dependent names is important because:

Such names are unbound and are looked up at the point of the template instantiation ... in both the context of the template definition and the context of the point of instantiation

However, the only thing that the standard says is a dependent name is given in [temp.dep]/2, referring to unqualified function calls, basically in order to enable ADL to be fully effective for those function calls.

Are there any other dependent names besides those?

Consider some code like this, for example:

template <class T>
void foo(T t) {
    t.bar();
};

If one were to refer to bar as a "dependent name", would that be a technically incorrect use of the term, according to the standard?

like image 614
Brian Bi Avatar asked Nov 15 '22 16:11

Brian Bi


1 Answers

Thanks to Declarations and where to find them being accepted into C++23, there is now an explicit enumeration of categories of dependent names, which seems to cover the code in the question.

In N4919 [temp.dep.general]/2 it is stated that

[...] The component name of an unqualified-id (7.5.4.2) is dependent if

  • it is a conversion-function-id whose conversion-type-id is dependent, or
  • it is operator= and the current class is a templated entity, or
  • the unqualified-id is the postfix-expression in a dependent call.

And in [temp.dep.type]/5 the rules for when qualified names are dependent are given:

A qualified name (6.5.5) is dependent if

  • it is a conversion-function-id whose conversion-type-id is dependent, or
  • its lookup context is dependent and is not the current instantiation, or
  • its lookup context is the current instantiation and it is operator=, or
  • its lookup context is the current instantiation and has at least one dependent base class, and qualified name lookup for the name finds nothing (6.5.5).

Regarding the example of t.bar() given in the question, the name bar is described in the referenced section (6.5.5) ([basic.lookup.qual]) as a "member-qualified-name". Furthermore, [basic.lookup.qual]/2 explains that its "lookup context" is "the type of its associated object expression (considered dependent if the object expression is type-dependent)". Clearly t is type-dependent, and it is the lookup context for bar, so [temp.dep.type]/5.2 applies, and bar is indeed a dependent qualified name.

like image 150
Brian Bi Avatar answered Jan 19 '23 05:01

Brian Bi