gpt4 book ai didi

c++ - Bjarne 对这个 ADL 示例是否有错误,或者我有编译器错误?

转载 作者:IT老高 更新时间:2023-10-28 12:02:31 24 4
gpt4 key购买 nike

我正在阅读 The C++ Programming Language, 4th Edition (by Bjarne Stroustrup ) 关于 .这是引用(26.3.6,过度激进的 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>).

这里是 result编译器生成(C++11模式):

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

这要么是编译器中的错误,要么是书中的错误。标准是怎么说的?

更新:

我需要澄清一下。我知道正确的答案是使用 std::endl .问题是关于书中的文字。正如 Lachlan Easton 已经说过的,这不仅仅是一个错字。整个段落(可能)是错误的。如果这本书是由其他(鲜为人知的)作者写的,我可以接受这种错误,但我一直(现在仍然)怀疑,因为它是由 Bjarne 写的。

最佳答案

这不是编译器中的错误。 ADL 用于查找 functions 而不是 argumentsoperator<<是这里通过ADL通过查看参数std::cout找到的函数和(应该是什么)std::endl .

关于c++ - Bjarne 对这个 ADL 示例是否有错误,或者我有编译器错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18086292/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com