像这样将函数声明为变量的语义是什么:
int main() {
int foo();
std::cout << foo; // prints 1
}
编辑: 为什么这不会导致链接器错误?
最佳答案
如果你看this attempt to replicate your problem您将看到来自编译器的警告消息:
main.cpp:5:18: warning: the address of 'int foo()' will never be NULL [-Waddress]
指向函数的指针永远不能是空指针。 但是因为您只有一个原型(prototype)、一个声明,而不是任何实际的函数定义,所以编译器将其评估为“真”。
关于c++ - C++中函数作为变量的语义是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45206486/