Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm reading The C++ Programming Language, 4th Edition (by Bjarne Stroustrup) about argument-dependent-lookup. Here is the quote (26.3.6, Overaggressive ADL):

Argument-dependent lookup (often referred to as ADL) is very useful to avoid verbosity (14.2.4). For example:

#include <iostream>

int main()
{
    std::cout << "Hello, world" << endl; // OK because of ADL
}

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

And here's the result produced by the compiler (C++11 mode):

prog.cpp: In function ‘int main()’:
prog.cpp:4:36: error: ‘endl’ was not declared in this scope
 std::cout << "Hello, world" << endl;
                                ^

Either this is a bug in the compiler or in the book. What does the standard say?

Update:

I need to clarify a bit. I know that the right answer is to use std::endl. The question was about the text in the book. As Lachlan Easton already said, it is not just a typo. The whole paragraph is (probably) wrong. I can accept this kind of error if the book is by an other (lesser known) author, but I was (and still am) in doubt because it was written by Bjarne.

like image 213
maverik Avatar asked Aug 06 '13 17:08

maverik


4 Answers

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.

like image 155
Peter Alexander Avatar answered Oct 23 '22 00:10

Peter Alexander


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

like image 49
Lachlan Easton Avatar answered Oct 22 '22 23:10

Lachlan Easton


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.

like image 20
Ali Avatar answered Oct 22 '22 22:10

Ali


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.

like image 10
Lightness Races in Orbit Avatar answered Oct 22 '22 23:10

Lightness Races in Orbit