I was looking at some code a friend sent me, and he said: "It compiles, but doesn't work". I saw that he used the functions without the parentheses, something like this:
我正在查看一位朋友发给我的一些代码,他说:“它可以编译,但不能工作。”我看到他用的函数没有括号,像这样
void foo(){
cout<< "Hello world\n";
}
int main(){
foo; //function without parentheses
return 0;
}
The first I said was "use parentheses, you have to". And then I tested that code - it does compile, but when executed doesn't work (no "Hello world" shown).
我说的第一句话是“用圆括号,你必须这么做”。然后我测试了这段代码——它确实可以编译,但是当执行时就不能工作了(没有显示“Hello world”)。
So, why does it compile (no warning at all from the compiler GCC 4.7), but doesn't work?
那么,为什么它要编译(编译器GCC 4.7完全没有警告),但是却不能工作?
3 个解决方案
#1
12
It surely warns if you set the warning level high enough.
如果你把警戒线设置得足够高,它肯定会发出警告。
A function name evaluates to the address of the function, and is a legal expression. Usually it is saved in a function pointer,
函数名计算到函数的地址,是合法的表达式。通常它保存在函数指针中,
void (*fptr)() = foo;
but that is not required.
但这不是必需的。
#2
11
You need to increase the warning level that you use. foo;
is a valid expression statement (the name of a function converts to a pointer to the named function) but it has no effect.
您需要增加您使用的警告级别。foo;是一个有效的表达式语句(函数的名称转换为指向已命名函数的指针),但没有效果。
I usually use -std=c++98 -Wall -Wextra -pedantic
which gives:
我通常使用-std=c++98 -Wall -Wextra -pedantic
<stdin>: In function 'void foo()':
<stdin>:2: error: 'cout' was not declared in this scope
<stdin>: In function 'int main()':
<stdin>:6: warning: statement is a reference, not call, to function 'foo'
<stdin>:6: warning: statement has no effect
#3
3
foo;
You're not actually 'using' the function here. You're just using the address of it. In this case, you're taking it but not really using it.
你实际上并没有使用这个函数。你只是在用它的地址。在这种情况下,你拿了它,但没有用它。
Addresses of functions (i.e. their names, without any parenthesis) are useful when you want to pass that function as a callback to some other function.
当您希望将函数作为回调传递给其他函数时,函数的地址(即它们的名称,没有括号)是有用的。
#1
12
It surely warns if you set the warning level high enough.
如果你把警戒线设置得足够高,它肯定会发出警告。
A function name evaluates to the address of the function, and is a legal expression. Usually it is saved in a function pointer,
函数名计算到函数的地址,是合法的表达式。通常它保存在函数指针中,
void (*fptr)() = foo;
but that is not required.
但这不是必需的。
#2
11
You need to increase the warning level that you use. foo;
is a valid expression statement (the name of a function converts to a pointer to the named function) but it has no effect.
您需要增加您使用的警告级别。foo;是一个有效的表达式语句(函数的名称转换为指向已命名函数的指针),但没有效果。
I usually use -std=c++98 -Wall -Wextra -pedantic
which gives:
我通常使用-std=c++98 -Wall -Wextra -pedantic
<stdin>: In function 'void foo()':
<stdin>:2: error: 'cout' was not declared in this scope
<stdin>: In function 'int main()':
<stdin>:6: warning: statement is a reference, not call, to function 'foo'
<stdin>:6: warning: statement has no effect
#3
3
foo;
You're not actually 'using' the function here. You're just using the address of it. In this case, you're taking it but not really using it.
你实际上并没有使用这个函数。你只是在用它的地址。在这种情况下,你拿了它,但没有用它。
Addresses of functions (i.e. their names, without any parenthesis) are useful when you want to pass that function as a callback to some other function.
当您希望将函数作为回调传递给其他函数时,函数的地址(即它们的名称,没有括号)是有用的。