c++ - C++中函数作为变量的语义是什么

标签 c++ linker-errors

像这样将函数声明为变量的语义是什么:

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/

相关文章:

c++ - 如何调用堆栈展开的函数?

c++ - 分布式计算的 Tensorflow 设置

c++ - 链接时奇怪的 undefined reference

c++ - 无法从另一个模板类调用模板类的模板成员函数

c++ - Cinder 如何纹理 .obj trimesh

c++ - MSVC 友元函数声明错误

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

c++ - 静态库多重定义链接错误

c++ - C 中的链接器错误 : undefined Reference to "method" c

ios - Xcode:架构 i386 的 undefined symbol :为 iOS 模拟器构建 Unity 项目时的 DebugStringToFile