Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么ostream iomanip在传递给运算符时不需要模板参数<&书信电报;?_C++_Templates_Iostream - Fatal编程技术网

C++ 为什么ostream iomanip在传递给运算符时不需要模板参数<&书信电报;?

C++ 为什么ostream iomanip在传递给运算符时不需要模板参数<&书信电报;?,c++,templates,iostream,C++,Templates,Iostream,在gnu stdc++头ostream的名称空间std中,这是std::endl的定义: template<typename _CharT, typename _Traits> inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) { return flush(__os.put(__os.widen('\n'))

在gnu stdc++头ostream的名称空间std中,这是std::endl的定义:

template<typename _CharT, typename _Traits>
    inline basic_ostream<_CharT, _Traits>&
    endl(basic_ostream<_CharT, _Traits>& __os)
    { return flush(__os.put(__os.widen('\n'))); }
模板
内联基本组&
endl(基本团队和操作系统)
{return flush(uu os.put(u os.gaveled('\n'));}
为什么可以将操纵器作为函数指针来编写
std::cout。在像
std::endl
这样传递模板时执行,因此不需要显式指定模板参数

如果可能,编译器将从函数参数推断缺少的模板参数。当尝试函数调用和获取函数模板的地址时,会发生这种情况

这种机制使使用模板操作符成为可能,因为 没有为其他运算符指定模板参数的语法 而不是将其重新编写为函数调用表达式

#include <iostream>
int main() 
{
    std::cout << "Hello, world" << std::endl;
    // operator<< is looked up via ADL as std::operator<<,
    // then deduced to operator<<<char, std::char_traits<char>> both times
    // std::endl is deduced to &std::endl<char, std::char_traits<char>>
}
#包括
int main()
{

std::cout作为参考,
basic_ostream
定义了这个成员函数:
basic_ostream&operator