返回void类型在C和C ++ [英] Return void type in C and C++
问题描述
这编译没有任何警告。
这是合法的C和C ++或者它只是在海湾合作委员会的工作,并铛?
如果它是合法的,是经过C99一些新的东西吗?
无效F(){}无效F2(){
返回f()的;
}
更新
为拉德凌志建议我尝试这样做:
$ GCC -Wall -Wpedantic -c x.c
x.c:在函数'F2':
x.c:7:9:警告:ISO C不允许回归与前pression,在函数返回void [-Wpedantic]
返回f()的;
$铛-Wall -Wpedantic -c x.c
x.c:7:2:警告:void函数'F2'不应该返回void前pression [-Wpedantic]
返回f()的;
^ ~~~~~
1警告产生。
$ GCC -Wall -Wpedantic -c x.cc
(没有错误)
$铛-Wall -Wpedantic -c x.cc
(没有错误)
更新
有人问这样的结构是如何帮助。那么是多了还是语法糖少。这里有一个很好的例子:
无效error_report(为const char * S){
的printf(%s错误\\ n,S);
出口(0);
}空隙过程(){
如果(步骤1()== 0)
返回error_report(第1步); 开关(步骤2()){
情况下0:返回error_report(第2步 - 无内存);
案例1:返回error_report(第2步 - 内部错误);
} 的printf(处理完成\\ n!);
}
C11 ,6.8.6.4的收益
语句:
A
收益
和前pression声明不得出现在它的返回类型的函数是无效
块引用>否后,您可能无法使用一个前pression,哪怕是
无效
键入从同一文件的前言:
在第二版主要变化包括:
[...]
收益
不前pression不是在函数返回一个值(反之亦然)允许
块引用>因此,这是从C89的变化 - > C99(语言标准第二版),并从那时起一直这样
。C ++ 14 ,6.6.3的
收益
语句
非void类型的前pression的return语句可以在返回值的函数只用[...]
与类型为void的前pression的return语句只能用在功能与简历返回类型
无效;只是在函数返回到其调用者之前的前pression进行评估。
块引用>是,您可以使用一个前pression的如果的是void类型的(因为这一直是有效的C ++ 98)。
This compiles without any warnings.
Is this legal in C and C++ or does it just work in gcc and clang?
If it is legal, is it some new thing after C99?
void f(){ } void f2(){ return f(); }
Update
as "Rad Lexus" suggested I tried this:
$ gcc -Wall -Wpedantic -c x.c x.c: In function ‘f2’: x.c:7:9: warning: ISO C forbids ‘return’ with expression, in function returning void [-Wpedantic] return f();
$ clang -Wall -Wpedantic -c x.c x.c:7:2: warning: void function 'f2' should not return void expression [-Wpedantic] return f(); ^ ~~~~~ 1 warning generated.
$ gcc -Wall -Wpedantic -c x.cc (no errors)
$ clang -Wall -Wpedantic -c x.cc (no errors)
Update
Someone asked how this construction is helping. Well is more or less syntactic sugar. Here is one good example:
void error_report(const char *s){ printf("Error %s\n", s); exit(0); } void process(){ if (step1() == 0) return error_report("Step 1"); switch(step2()){ case 0: return error_report("Step 2 - No Memory"); case 1: return error_report("Step 2 - Internal Error"); } printf("Processing Done!\n"); }
解决方案C11, 6.8.6.4 "The
return
statement":A
return
statement with an expression shall not appear in a function whose return type isvoid
.No, you may not use an expression, even if it is of
void
type.From the foreword of the same document:
Major changes in the second edition included:
[...]
return
without expression not permitted in function that returns a value (and vice versa)So this was a change from C89 -> C99 (the second edition of the language standard), and has been that way ever since.
C++14, 6.6.3 "The
return
statement":A return statement with an expression of non-void type can be used only in functions returning a value [...] A return statement with an expression of type 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.
Yes, you may use an expression if it is of void type (that's been valid since C++98).
这篇关于返回void类型在C和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!