以下示例编译(虽然 VS2010 C++ 编译器发出警告 C4353)并且表达式 (*) 计算为 0
:
#include <iostream>
#include <string>
int main()
{
0(1, "test"); // (*) - any number and type of arguments allowed
int n = 0(1, "test"); // 0
std::string str(0(1, "test")); // Debug assertion fails - 0 pointer passed
}
是否使用 0
作为 C++ 标准允许/规定的函数名称,或者它的解析是特定于编译器的?我正在查看 N3242 草案,但找不到任何内容与此有关。 Microsoft 编译器显然将此类构造(或带有 __noop
的构造)解析为值为 0
的整数。
警告 C4353:
warning C4353: nonstandard extension used: constant 0 as function expression. Use __noop function intrinsic instead
最佳答案
函数名是一个标识符,标识符需要以非数字开头(§2.11):
identifier:
identifier-nondigit
identifier identifier-nondigit
identifier digit
关于c++ - 零作为函数名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10227997/