Returning void

在一个 void 函数中返回void是不是允许的?这是一个比较有意思的问题。为此我查了一下C99和C++标准,它们的规定是不同的。

在标准C中,这是不允许的,参见C99 6.8.6.4:

A return statement with an expression shall not appear in a function whose return type
is void. A return statement without an expression shall only appear in a function
whose return type is void.
写得很清楚,没什么好说的。C++不同,C++标准是完全允许的,第6.6.3节第三段写道:
A return statement with an expression of type “cv void”can be used only in functions
with a return type of cv void; the expression is evaluated just before the function
returns to its caller.
我想C++之所以这么做是为了模板考虑的,看下面的代码片段:
[cpp]
template
T func(T (*pf)())
{
return pf();
}

void foo() {}

int main()
{
func(foo);
return 0;
}
[/cpp]