developer tip

C 및 C ++에서 void 유형 반환

copycodes 2020. 11. 18. 09:19
반응형

C 및 C ++에서 void 유형 반환


이것은 경고없이 컴파일됩니다.

이것이 C 및 C ++에서 합법적입니까? 아니면 gcc 및 clang에서만 작동합니까?

합법적이라면 C99 이후 새로운 것이 있습니까?

void f(){

}

void f2(){
    return f();
}

최신 정보

"Rad Lexus"가 제안한대로 다음을 시도했습니다.

$ 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)

최신 정보

누군가이 건축이 어떻게 도움이되는지 물었습니다. 글쎄요 다소 구문 론적 설탕입니다. 다음은 좋은 예입니다.

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 " return성명서":

return표현식이 있는 문은 반환 유형이 인 함수에 나타나지 않습니다 void.

아니요 , void유형 이더라도 표현식을 사용할 수 없습니다 .

동일한 문서의 서문에서 :

두 번째 버전의 주요 변경 사항은 다음과 같습니다.

[...]

  • return 값을 반환하는 함수에서는 표현식이 허용되지 않습니다 (반대의 경우도 마찬가지).

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).


This code is allowed in C++ but not allowed in C

From Return statement @ cppreference

In a function returning void, the return statement with expression can be used, if the expression type is void.


OTOH in C11 specs draft n1570:

Major changes in the second edition included:

return without expression not permitted in function that returns a value (and vice versa)

(return with expression not permitted in function that returns a void)

and 6.8.6.4 return

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.

(even if the expression evaluates to void)


C++ allows something like that:

void f() 
{
    return void();
}

While C does not. That's why a warning is issued if you compile it a ISO C rather than ISO C++. This is formally described as:

A return statement with an expression of type void can be used only in functions with a return type of cv void


ISO/IEC 9899:201x Committee draft says the following:

6.8.6.4 The return statement

Constraints

  1. 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.

So, it is forbidden in C.


You need to use -pedantic switch to gcc for it to complain about standard violations:

test.c: In function ‘f2’:
test.c:6:12: warning: ISO C forbids ‘return’ with expression, in function returning void 
            [-Wpedantic]
     return f();

Standard C does not support this construction:

C11 6.8.6.4: The return statement

Constraints

1 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.

No special provisions are added for the special case in the question. Some C compilers do support this as an extension (gcc does, unless instructed to conform to one of the C Standards), but C11 and previous versions consider it a constraint violation.

참고URL : https://stackoverflow.com/questions/35987493/return-void-type-in-c-and-c

반응형