C++ endl()不含std::前缀或使用语句。

问题描述 投票:0回答:1

为什么要使用 endl() 没有命名空间限定符的编译? 我在多个编译器上试过,在这里搜索了好几页 "endl without std::",我很疑惑,为什么连警告都没有。

#include <iostream>

int main(){
    endl(std::cout << "hello weirdness");
    return 0;
}

这是从另一个网站上找到的一点刻意混淆的代码中提炼出来的,是 我在真正的程序中会用到的东西。 我只是不明白为什么它不抱怨说 endl 并没有定义。

c++ c++14
1个回答
7
投票

std::endl 是一个函数模板,其签名如下。

template <class charT, class traits>
std::basic_ostream<charT, traits>& endl(std::basic_ostream<charT, traits>& os);

当参数的类型是... std::ostream, 依赖于参数的查询 发生,并且函数名称 endl 是在命名空间 std.

© www.soinside.com 2019 - 2024. All rights reserved.