Link to home
Start Free TrialLog in
Avatar of Zoppo
ZoppoFlag for Germany

asked on

Isn't this allowed in C++ at all or only in VC++...

Hi all,

while I tried to implement some template functions I encountered
a problem which can be reduced to followinf code, which
produces a compile error in VC++ 6.0:

------------------------
void foo_0()
{
}

void foo_1()
{
 return foo_0();
}
------------------------

I don't really understand the reason why it shouldn't be
possible to do this.

Can anybody explain this?

Thanks in advance,

ZOPPO
Avatar of cryptosid
cryptosid

hello zoppo,

the problem here lies in the return type of foo_0()
its void so it is not supposed to return any value.

and u have used the statement

return foo_0();

which means 'RETURN the VALUE RETURNED by foo_0()

but since foo_0() is declared as void the compiler maintains in its tables that the function will not return a value

so the compiler faults on the statement 'return foo_0()'
since u are asking a 'void function to return a value'

i hope i solved ur prob
cheers
Avatar of Zoppo

ASKER

not really, sorry...

In any other case, if return type of both functions differs
from 'void' the compiler does a type check if the type to
be returned is the same type as the type declared as return
type.

Only in case that declared return type is 'void' the compiler
simply seems to check if anything follows the return without
checking the type.

And, regarding the types the code should compile without any
problems since the returned type 'void' is the same as the
declared return type 'void'.

So, question may be: Is 'void' a type or is it more like a
special handled keyword?

ZOPPO
yes i forgot to mention that ... since u declared ur second function foo_1() as void it means the function should not return anything...

since 'void' means 'nothing'

and u have used the return statement with an argument foo_0();

so that is the main reason of the error....
'void functions should not return anything' not even a call to void function!

so change the statement to simply

'return;'
or else if u wanna make sure what i had told in the earlier comment is correct then try chaning the data type of the function foo_1() to int

you will find what i am saying is true, sorry earlier on i forgot all about the void foo_1()...

cheers

Sorry i had forgot earlier that u have used

void foo_1()

'void' means 'nothing' ... and function that is declared as void cannot return anything.

so although u have called another 'void' function foo_0() which doesn't return anything...it doesn't matter the compiler isn't smart enough to understand that the function foo_0() is returning 'nothing'.

The compiler simply sees that the return statement of a void function should not contain any argument whatsoever is the type of the argument...

so that is the reason the statement 'return foo_1();' doesn't work...

Well and yes about my earlier comment try changing the data type of foo_1() to 'int' and then compile the code
you will find whatever i was saying is true...

cheers
Avatar of Zoppo

ASKER

>and u have used the return statement with an argument foo_0();
that's right, but the value returned by foo_0() is of type void either...


Maybe I should show you a sample as I want to use it with a template:

int foo_0()
{
//...
  return 0;
}

void foo_1()
{
//...
  return;
}


template <typename _RET>
_RET callFunc( _RET(*pFunc)() )
{
//...
  return (*pFunc)();
}

int main(int argc, char* argv[])
{
  callFunc( &foo_0 ); // ok, compiles fine
  callFunc( &foo_1 ); // causes a compile error in callFunc
}


That's nearly what I wanna do ... and, if it's not possible
to do this with functions returning void I can't use it as
I want.




> The compiler simply sees that the return statement of a
> void function should not contain any argument whatsoever
> is the type of the argument...
And so my question is still: Is this only my compiler which
handles it like this or is the compiler error correct regarding
C++ specs?


ZOPPO
Avatar of Zoppo

ASKER

...

That's what I would expect to be possible:

int main(int argc, char* argv[])
{
 int i = callFunc( &foo_0 ); // ok, compiles fine
 callFunc( &foo_0 ); // ok, compiles fine either
 callFunc( &foo_1 ); // should compile fine
 int j = callFunc( &foo_1 ); // should produce a compiler error
}

ZOPPO
no its not a compiler error its C++ syntax...
Avatar of Zoppo

ASKER

Are you sure? Can you post me any link to C++ specs where this is stated?
Check the Function definition and Calls section...

http://www.husseinsspace.com/teaching/cnotes/chaptwo.htm
hello zoppo,

did i solve ur prob ?
Avatar of Zoppo

ASKER

hmm ... it only tells me that a function which is declared
to return 'void' returns nothing ... that's already clear.


What I meanwhile found so far in C++ specs is:
>the  void  type  is an incomplete type (_basic.fundamental_). Objects
>shall not be defined to have an incomplete type.


>The  void type has an empty set of values.  The void type
>is an incomplete type that cannot be completed.  It is used
>as  the  return  type for  functions  that  do  not  return
>a value.  Any expression can be explicitly converted to
>type cv void (_expr.cast_).  An expression  of type void
>shall be used only as an expression statement (_stmt.expr_),
>as an operand of a comma expression (_expr.comma_), or as a
>second  or third operand of ?: (_expr.cond_).


>A return statement without an expression can be used only
>in functions that  do not return a value, that is, a
>function with the return value type   void,   a  
>constructor   (_class.ctor_),   or   a   destructor
>(_class.dtor_).   A  return  statement  with an expression
>can be used only in functions returning a value; the value
>of  the  expression  is returned  to  the caller of the
>function.  If required, the expression is implicitly
>converted to the return type of the function in which it
>appears.   A return statement can involve the construction
>and copy of a temporary object (_class.temporary_).  
>Flowing  off  the  end  of  a function  is  equivalent  to
>a  return with no value; this results in undefined behavior
>in a value-returning function.

(I found them at http://anubis.dkuug.dk/jtc1/sc22/open/n2356/ )

I think the last paragraph is the important one ... well, now
I found that it's part of the specs ... but, unfortunateley I
don't see the reason for this and wish it would be possible.



Unfortunateley I don't feel that anyone deserves the points for
this question since I found the wanted C++ spec myself.

I'll ask a question at Customer Support to remove the points
and move the question to the PAQ since it maybe other experts
will need this too.


have a nice day,

regards,

ZOPPO
well thanx but i wasn't hungry about points! its just the zeal and precise nature of ur query that got me into the act...

cheers always
well thanx but i wasn't hungry about points! its just the zeal and precise nature of ur query that got me into the act...

cheers always
Avatar of Zoppo

ASKER

Fine :)

see you at the next Q ...
Here is the MSDN description:

"The C++ return Statement

The return statement allows a function to immediately transfer control back to the calling function (or, in the case of the main function, transfer control back to the operating system). The return statement accepts an expression, which is the value passed back to the calling function. Functions of type void, constructors, and destructors cannot specify expressions in the return statement; functions of all other types must specify an expression in the return statement."

The main point is that functions of type void cannot specify expressions in the return statement. I don't know if this is in the specification or just Microsoft's implementation of return.
ASKER CERTIFIED SOLUTION
Avatar of ComTech
ComTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Zoppo

ASKER

thanks, SteveGTR ... as I found in the above posted specs I
think it's a C++ thing, not just MS.

I still don't understand the reason coz I think it's a needless
inconsistency of the language ... maybe I try to ask Bjarne Stroustrup about his intention of this spec :o)

ZOPPO
you might just take out the 'return' statement.
Avatar of Zoppo

ASKER

Well, that does not solve the real problem ... please take
a look at the sample with the function template callFunc (5th comment)
The code you posted works just fine on the VC++ 7 compiler, so I assume that this is a bug with the VC++ 6 compiler...

I thought it might be possible to get the same effect in VC++ 6 with a specialization of the function template, something like:

template <>
void callFunc<void>( void (*pFunc)() )
{
   (*pFunc)();
}

however, the compiler doesn't like this either.  It may be a starting point, however...
Avatar of Zoppo

ASKER

Hi jasonclarke,

thanks for the comment ... after I read it I decided to try it
with GCC and it compiled and worked fine.

Therfore I think you deserver some points ... take a look at:
https://www.experts-exchange.com/questions/20339780/Points-for-jasonclarke.html


Have a nice day,

regards,

ZOPPO