为什么'std :: endl'在语句'std :: cout<< std :: endl;“,给定参数相关的查找? [英] Why does 'std::endl' require the namespace qualification when used in the statement 'std::cout << std::endl;", given argument-dependent lookup?

查看:214
本文介绍了为什么'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.

这篇关于为什么'std :: endl'在语句'std :: cout&lt;&lt; std :: endl;“,给定参数相关的查找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆