gnu.gcc.help
[Top] [All Lists]

Re: How do I get a warning or error when returning values from functions

Subject: Re: How do I get a warning or error when returning values from functions returning void in GCC?
From: Thomas Maeder <maeder@xxxxxxx>
Date: Thu, 17 Jul 2008 19:59:03 +0200
Newsgroups: gnu.gcc.help

bgutter <bgutter1@xxxxxxxxx> writes:

> The following code compiles with -Wall and -Werror with no error. This
> is on GCC 3.4.6 running on Red Hat Enterprise Linux 4 on a 32-bit
> Linux machine, nor will GCC 4.2.1 running on a 64-bit Linux machine
> emit an error or warning:
>
>     void f3(int value)
>     {
>         (void)value;
>         return;
>     }
>
>     void f2(int value)
>     {
>         return f3(value);
>     }
>
>     void f1()
>     {
>         f2(23);
>     }
>
> But this code does not compile, but fails with the error shown below:
>
>     void f4()
>     {
>         return 23;
>     }
>
>     Isrc/st.c: In function 'void f4()':
>     Isrc/st.c:35: error: return-statement with a value, in function
> returning 'void'
>
> Why?

In C++, returning a void expression was added to allow

template <typename T>
T f();

template <typename T>
T g()
{
  return f<T>();
}

int main()
{
  g<void>();
}


Not sure about C, though.

<Prev in Thread] Current Thread [Next in Thread>