Bug 46766 - Type 'void' is treated differently if used as return value or as parameter
Summary: Type 'void' is treated differently if used as return value or as parameter
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.5.1
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-12-02 13:57 UTC by Fredrik Hederstierna
Modified: 2010-12-02 17:10 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fredrik Hederstierna 2010-12-02 13:57:25 UTC
It is valid to return a void-function, or cast a variable to void, from a void-function.

This makes some sense, in particular in C++ since we might have a template, and we would like to put 'void' as type in this C++ template.

But then maybe it should also be allowed to put 'void' as inparameter to a void-function, but then compiler warns about too many arguments.

void.c: In function ‘main’:
void.c:16: error: too many arguments to function ‘f1’
void.c:17: error: too many arguments to function ‘f2’

Somehow it would be more 'aligned' to have function-return-values and function-in-parameters work the same way, so that template-alike-constructions, or similar pure C macro/preprocessor constructions, could work the same perhaps?

void f1(void)
{
  return (void)0; //OK
}

void f2(void)
{
  return f1(); //OK
}

int main(void)
{
  f1();        //OK
  f2();        //OK
  f1((void)0); //ERROR
  f2(f1());    //ERROR
  return 0;   
}
Comment 1 Andreas Schwab 2010-12-02 14:23:59 UTC
void f1(void) declares a function that takes no parameters.
Comment 2 Fredrik Hederstierna 2010-12-02 14:42:35 UTC
Ok, but also f1() declares that it does not return any parameters, still it can return (void)0;

I'm not saying either is wrong, I just though it should be consistent.

If its ok to _return_ (void) from a function, why is it not ok to have (void) as _inparameter_ to a function. Where is the logical difference?

/Fredrik
Comment 3 Jonathan Wakely 2010-12-02 15:14:14 UTC
(In reply to comment #0)
> 
> void f1(void)
> {
>   return (void)0; //OK

This is valid in C++ but allowing it in C is a GCC extension.

 
> void f2(void)
> {
>   return f1(); //OK

This is valid in C++ but allowing it in C is a GCC extension.


>   f1((void)0); //ERROR
>   f2(f1());    //ERROR

This is repulsive.

I don't see this as "more aligned" with the examples above, I see it as more aligned with:

int i = (int) f1();   // EURGH

Or consider the following, how many arguments are given to f3?

   void f3(int);
   f3((void)0, f1(), f2(), 3);  // EURGH
Comment 4 Fredrik Hederstierna 2010-12-02 15:55:05 UTC
Yes, I agree its EURGH.
I guess its not preferred to make C++ template-alike code in C.
I think its worth avoid stuff like:

#ifdef BLAH_BLAH_BLAH
  #define RETURN_TYPE_C_TEMPLATE  void
  #define PARAM_TYPE_C_TEMPLATE   void
  #define PARAM_TYPE_C_TEMPLATE_VARIABLE
#else
  #define RETURN_TYPE_C_TEMPLATE  int
  #define PARAM_TYPE_C_TEMPLATE   int
  #define PARAM_TYPE_C_TEMPLATE_VARIABLE  var
#endif

RETURN_TYPE_C_TEMPLATE func( PARAM_TYPE_C_TEMPLATE  PARAM_TYPE_C_TEMPLATE_VARIABLE )
{
  return (RETURN_TYPE_C_TEMPLATE) template_func1( PARAM_TYPE_C_TEMPLATE_VARIABLE );
}

int main(void)
{
  func((PARAM_TYPE_C_TEMPLATE) template_func2());  
  return ERR_EURGH;
}
Comment 5 Andreas Schwab 2010-12-02 17:10:15 UTC
If you want the standard to be changed then this is the wrong place.