函数作为变量的语义是什么 C++.
What are the semantics of function as variable in C++
像这样将函数声明为变量的语义是什么:
int main() {
int foo();
std::cout << foo; // prints 1
}
编辑: 为什么这不会导致链接器错误?
如果您查看此复制问题的尝试,您将看到来自编译器的警告消息:
main.cpp:5:18: warning: the address of 'int foo()' will never be NULL [-Waddress]
指向函数的指针永远不能是空指针。但是,由于您所拥有的只是一个原型,一个声明,而不是任何实际的函数定义,因此编译器将其评估为"true"。
在 clang 编译器中编译程序C++并看到警告:
Warning(s):
source_file.cpp:5:12: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
int foo();
^~
source_file.cpp:5:12: note: replace parentheses with an initializer to declare a variable
int foo();
^~
= 0
source_file.cpp:6:18: warning: address of function 'foo' will always evaluate to 'true' [-Wpointer-bool-conversion]
std::cout << foo; // prints 1
~~ ^~~
source_file.cpp:6:18: note: prefix with the address-of operator to silence this warning
std::cout << foo; // prints 1
^
&
2 warnings generated.
为什么输出 1?
因为根据函数的警告地址,"foo"将始终计算为"true"。
相关文章:
- 为不同配置设置MSVC_RUNTIME_LIBRARY的正确方法是什么
- C++避免重复声明的语法是什么
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 实现无开销push_back的最佳方法是什么
- C++从另一个类访问公共静态向量的正确方法是什么
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 在C++17中,引用const字符串的语义应该是什么
- 在C++中使用移动语义的正确方法是什么?
- 函数作为变量的语义是什么 C++.
- testb $1, %al 的语义是什么
- 模板化代码的语法和语义是什么C++
- C++11 中已删除成员函数的确切语义是什么?
- 消除默认/删除移动/复制语义中涉及的样板的好方法是什么
- 在共享库中全局声明的非pod对象的语义是什么?
- 语义和顶点布局在D3D11中的意义是什么?
- 移动语义 - 它的全部内容是什么?
- 指向已分配对象的指针没有值语义是什么意思
- 在遵循pimpl设计模式的类中实现move语义的正确方法是什么?
- 我是否正确地使用了move语义?好处是什么?
- "值语义"和"指针语义"是什么意思