Re: void func () returning value?



 DEVELOP > c-Plus-Plus > Re: void func () returning value?

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Nick Keighley"
Date: 18 Oct 2006 02:41:34 AM
Object: Re: void func () returning value?
Harald van D=C4=B3k wrote:

Yevgen Muntyan wrote:

Consider the following code:

void func (void)
{
}

void func2 (void)
{
return func ();
}

gcc -pedantic says the following:

muntyan@munt10:/tmp$ gcc -pedantic -c file.c
file.c: In function =E2=80=98func2=E2=80=99:
file.c:7: warning: =E2=80=98return=E2=80=99 with a value, in function r=

eturning void


If I recall correctly, compiler in some Microsoft Visual Studio version
told me the same thing. So, is this a compiler bug (feature), or is this
"return func ();" invalid in standard C?


It's invalid in standard C. Perhaps you're thinking of C++, where it is
allowed?

since when has it been valid for a void function to return a value, in
either C or C++?
--=20
Nick Keighley
.

User: "Risto Lankinen"

Title: Re: void func () returning value? 18 Oct 2006 03:01:21 AM
"Nick Keighley" <nick_keighley_nospam@hotmail.com> wrote in message
news:1161157294.584254.312760@h48g2000cwc.googlegroups.com...

since when has it been valid for a void function to return a value, in
either C or C++?

A void function can "return" an expression whose type is void . This
is to facilitate templates such as...
template <typename T> T func() { return static_cast<T>(expr); }
.... to be instantiable also for T=void.
- Risto -
--
Nick Keighley
.

User: "Greg"

Title: Re: void func () returning value? 18 Oct 2006 02:53:38 AM
Nick Keighley wrote:

Harald van D=C4=B3k wrote:

Yevgen Muntyan wrote:


Consider the following code:

void func (void)
{
}

void func2 (void)
{
return func ();
}

gcc -pedantic says the following:

muntyan@munt10:/tmp$ gcc -pedantic -c file.c
file.c: In function =E2=80=98func2=E2=80=99:
file.c:7: warning: =E2=80=98return=E2=80=99 with a value, in function=

returning void


If I recall correctly, compiler in some Microsoft Visual Studio versi=

on

told me the same thing. So, is this a compiler bug (feature), or is t=

his

"return func ();" invalid in standard C?


It's invalid in standard C. Perhaps you're thinking of C++, where it is
allowed?


since when has it been valid for a void function to return a value, in
either C or C++?

It's not legal in C++ for a "return" statement of a void function to
return a value [=C2=A76.6.3]. The above program is nonetheless OK since it
is legal in C++ for a return statement in a void function to have a
(cv) void type expression - as in this case, the return expression is
simply a call to another void function.
Greg
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER