C++ - 语句无法解析重载函数的地址

Sim*_*ity 20 c++ function resolve

当我将以下内容键入独立行时:

std::endl;

我收到以下错误:

statement cannot resolve address for overloaded function

这是为什么?我不能写成std::endl;独立的线路吗?

谢谢.

xto*_*ofl 16

std::endl是一个功能模板.通常,它用作插入运算符的参数<<.在那种情况下,所operator<<讨论的流的定义将被定义为例如ostream& operator<< ( ostream& (*f)( ostream& ) ).f定义了参数的类型,因此编译器将知道函数的确切重载.

它与此相当:

void f( int ){}
void f( double ) {}
void g( int ) {}
template<typename T> void ft(T){}

int main(){
  f; // ambiguous
  g; // unambiguous
  ft; // function template of unknown type...
}
Run Code Online (Sandbox Code Playgroud)

但您可以通过某些类型提示解决歧义:

void takes_f_int( void (*f)(int) ){}

takes_f_int( f ); // will resolve to f(int) because of `takes_f_int` signature
(void (*)(int)) f; // selects the right f explicitly 
(void (*)(int)) ft; // selects the right ft explicitly 
Run Code Online (Sandbox Code Playgroud)

这是通常std::endl在作为参数提供时发生的operator <<:有一个函数的定义

 typedef (ostream& (*f)( ostream& ) ostream_function;
 ostream& operator<<( ostream&, ostream_function )
Run Code Online (Sandbox Code Playgroud)

这将使编译器std::endl在提供给例如时选择正确的重载std::cout << std::endl;.

好问题!