Is Bjarne wrong about this example of ADL, or do I have a compiler bug?

Solution 1:

It's not a bug in the compiler. ADL is used to lookup functions not arguments. operator<< is the function found through ADL here by looking at the parameters std::cout and (what should be) std::endl.

Solution 2:

For those saying it's a typo, it's not. Either Bjarne made a mistake or the compiler has it wrong. The paragraph after the one posted by OP reads

Without argument-dependent lookup, the endl manipulator would not be found. As it is, the compiler notices that the first argument to << is an ostream defined in std. Therefore, it looks for endl in std and finds it (in<iostream>).

Solution 3:

It is a typo in the book as the others have already pointed out. However, what is meant in the book is that we would have to write

std::operator<<(std::cout, "Hello, world").operator<<(std::endl);

without ADL. That's what Bjarne meant by verbosity.


I stand corrected. As Lachlan Easton points out, it isn't a typo but a mistake in the book. I don't have access to this book that's why I couldn't read that paragraph and realize it myself. I have reported this mistake to Bjarne so that he can correct it.


Funny. The same example is on Wikipedia and

Note that std::endl is a function but it needs full qualification, since it is used as an argument to operator<< (std::endl is a function pointer, not a function call).

No doubt, it is a mistake in the book. Nevertheless the example std::operator<<(std::cout, "Hello, world").operator<<(std::endl); shows how ADL helps reducing the verbosity.


Thanks to gx_ for pointing out my mistake.

Solution 4:

The hint is in the name "argument-dependent lookup".

It's lookup for unqualified function names, that works depending on the arguments.

It's got nothing to do with lookup for arguments.

Bjarne misspoke.

Solution 5:

I don't have the book, but this seems to be an error in the book, the fact that it's missing the namespace qualifier has nothing to do with ADL. It should be std::endl.