C++ 为什么ostream iomanip在传递给运算符时不需要模板参数<&书信电报;?
在gnu stdc++头ostream的名称空间std中,这是std::endl的定义: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'))
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