Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does 'std::endl' require the namespace qualification when used in the statement 'std::cout << std::endl;", given argument-dependent lookup?

I was looking at the Wikipedia entry on argument-dependent lookup, and (on Jan 04, 2014) the following example was given:

#include<iostream>

int main() 
{
  std::cout << "Hello World, where did operator<<() come from?" << std::endl;
}

... with the following comment:

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

My thought is that the comment is incorrect (or simply unclear). I am considering changing the comment to say, instead

Note that std::endl needs full qualification, because ADL does not apply to the arguments of a function call; it only applies to the function name itself.

Am I correct that the Wikipedia comment is incorrect? Is my proposed change correct? (I.e., is my understanding of ADL correct in this example?)

like image 952
Dan Nissenbaum Avatar asked Jan 04 '14 20:01

Dan Nissenbaum


1 Answers

There's nothing wrong about what Wikipedia says.

std::cout << "Hello World, where did operator<<() come from?" << std::endl

is equivalent to the following (assuming operator<< is implemented as a free function)

operator<<(
    operator<<(std::cout, "Hello World, where did operator<<() come from?"),
    std::endl)

which clearly requires namespace qualification for both cout and endl because this is argument-dependent lookup (of the function), not "argument lookup".
The arguments determine the function to be called, not the way around.

like image 114
user541686 Avatar answered Oct 05 '22 22:10

user541686