返回void类型在C和C ++ [英] Return void type in C and C++

查看:289
本文介绍了返回void类型在C和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 is void.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆