为什么'std :: endl'在语句'std :: cout<< std :: endl;“,给定参数相关的查找?
我在查看与参数相关的查询的维基百科词条,以及(Jan 04,2014)给出了以下示例:
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:
注意,std :: endl是一个函数,但它需要完全限定,
,因为它被用作运算符<< (std :: endl是一个函数
指针,而不是函数调用)。
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
请注意,std :: endl需要完全限定,
因为ADL不应用于函数调用的参数; it
仅适用于函数名称本身。
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?)
维基百科说的没什么问题。
There's nothing wrong about what Wikipedia says.
std::cout << "Hello World, where did operator<<() come from?" << std::endl
相当于以下内容(假设 operator 实现为免费函数)
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)
这显然需要 cout
和 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.