c++ - I need some clarification regarding this example on Stroustrup's new book about ADL -


i reproduce below argument-dependent lookup (adl) example given in pages 396 , 397 of stroustrup book (4th edition):

namespace n {     struct s { int i; };     void f(s);     void g(s);     void h(int); };  struct base {     void f(n::s); };  struct d : base {     void mf(n::s);      void g(n::s x)     {         f(x);   // call base::f()         mf(x);  // call d::mf()         h(1);   // error: no h(int) available     } }; 

what comments above correct (i've tested it), doesn't seem agree author says in next paragraph:

in standard, rules argument-dependent lookup phrased in terms of associated namespaces (iso §3.4.2). basically:

  • if argument class member , associated namespaces class (including base classes) , class's enclosing namespaces.
  • if argument member of namespace, associated namespaces enclosing namespaces.
  • if argument built-in type, there no associated namespaces.

in example, x, has type n::s not member of class d, nor of base base. it's member of namespace n. according second bullet above, function n::f(s) should 1 called, instead of base::f().

the result above doesn't seem agree second bullet in paragraph 3.4.2p2 in standard, says:

if t class type (including unions), associated classes are: class itself; class of member, if any; , direct , indirect base classes. associated namespaces namespaces of associated classes members. furthermore, if t class template specialization, associated namespaces , classes include: namespaces , classes associated types of template arguments provided template type parameters (excluding template template parameters); namespaces of template template arguments members; , classes of member templates used template template arguments members.

3.4.2/3 let x lookup set produced unqualified lookup (3.4.1) , let y lookup set produced argument dependent lookup (defined follows). if x contains

  • a declaration of class member, or
  • a block-scope function declaration not using-declaration, or
  • a declaration neither function or function template

then y empty. otherwise...

so basically, adl doesn't kick in when ordinary lookup finds member function or local (block-scope) function declaration (or that's not function). kick in when ordinary lookup finds stand-alone namespace-scope function, or when finds nothing @ all.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -