一个好奇的问题。
以下简短程序在屏幕上打印出1
。我想知道如何解释这种行为。
#include <iostream>
#include <string>
void foo() {}
int main()
{
std::cout << foo;
}
为便于参考,我在下面列出了cplusplus上的std::ostream::operator<<
签名。
但是我不太了解哪个重载版本用于功能(操作符)匹配,并且为什么打印1
。请让我知道您的想法。
arithmetic types (1)
ostream& operator<< (bool val);
ostream& operator<< (short val);
ostream& operator<< (unsigned short val);
ostream& operator<< (int val);
ostream& operator<< (unsigned int val);
ostream& operator<< (long val);
ostream& operator<< (unsigned long val);
ostream& operator<< (long long val);
ostream& operator<< (unsigned long long val);
ostream& operator<< (float val);
ostream& operator<< (double val);
ostream& operator<< (long double val);
ostream& operator<< (void* val);
stream buffers (2)
ostream& operator<< (streambuf* sb );
manipulators (3)
ostream& operator<< (ostream& (*pf)(ostream&));
ostream& operator<< (ios& (*pf)(ios&));
ostream& operator<< (ios_base& (*pf)(ios_base&));
顺便说一下,我在Windows 10(带有g++ 8.1
)和Ubuntu 18.04 LTS(带有g++ 9.1
)上都编译了程序。两者都打印1
。