Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > can you return a void function call?

Reply
Thread Tools

can you return a void function call?

 
 
deanbrown3d@yahoo.com
Guest
Posts: n/a
 
      03-04-2005
Hi there!

Suppose I have a function

void ShowMessage(string s);

(that has a void return type.)


In my other function F, also with a void return type, can I do this?

void F(int i)
{
// Can I return this?
return ShowMessage("Hi Dean"); // ????

}

I can seem to be able to do this in Borland Builder6, but I didn't
think it was possible until recently. Is this a compiler-specific
setting?


THX!

Dean

 
Reply With Quote
 
 
 
 
Gary Labowitz
Guest
Posts: n/a
 
      03-04-2005
<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi there!
>
> Suppose I have a function
>
> void ShowMessage(string s);
>
> (that has a void return type.)
>
>
> In my other function F, also with a void return type, can I do this?
>
> void F(int i)
> {
> // Can I return this?
> return ShowMessage("Hi Dean"); // ????
>
> }
>
> I can seem to be able to do this in Borland Builder6, but I didn't
> think it was possible until recently. Is this a compiler-specific
> setting?


No, this doesn't make sense. It is invalid to return a value from a function
returning void.
--
Gary


 
Reply With Quote
 
 
 
 
deanbrown3d@yahoo.com
Guest
Posts: n/a
 
      03-04-2005
Even if the value is void?

 
Reply With Quote
 
deanbrown3d@yahoo.com
Guest
Posts: n/a
 
      03-04-2005
I mean, it seems to make sense to say:

return ShowMessage("Hi"); // One line

then to write:

{
ShowMessage("Hi"); // Four lines
return;
}

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      03-04-2005
Gary Labowitz wrote:

>> In my other function F, also with a void return type, can I do this?
>>
>> void F(int i)
>> {
>> // Can I return this?
>> return ShowMessage("Hi Dean"); // ????
>>
>> }
>>
>> I can seem to be able to do this in Borland Builder6, but I didn't
>> think it was possible until recently. Is this a compiler-specific
>> setting?

>
> No, this doesn't make sense. It is invalid to return a value from a
> function returning void.


Nonsense! It is perfectly valid to do that, as long as the 'value' is of
type void:

void a()
{
}

void b()
{
return a();
}

The reason for this being allowed are templates. Think e.g. of the standard
library's mem_fun, which might use something like this (copied from
TC++PL):

template <class R, class T> class mem_fun_t : public unary_function<T*, R> {
R(T::*pmf)();
public:
explicit mem_fun_t(R(T::*p)()) : pmf(p) {}
R operator()(T* p) const { return (p->*pmf)(); }
};

Now if you have a member function returning void:

class X
{
public:
void foo() {};
};

int main()
{
std::mem_fun_t<void, X> fun = &X::foo;
fun(&X);
}

for 'fun', the operator becomes:

void operator()(X* p) const { return (p->*pmf)(); }

And (p->*pmf) becomes a function returning void.

 
Reply With Quote
 
Matthias Kaeppler
Guest
Posts: n/a
 
      03-04-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Even if the value is void?
>


void is not a value, void is nothing, so it doesn't make sense to return
anything. If you want to return something, then you'll have to use the
right signature for your function.
Invoking 'return' without passing a value just means that you are about
to leave the scope of the function.

--
Matthias Kaeppler
 
Reply With Quote
 
deanbrown3d@yahoo.com
Guest
Posts: n/a
 
      03-04-2005
Wow and there's the reason! Thanks Rolf, makes sense. It must have
always been that way, I just didn't notice.

Dean

 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      03-04-2005
"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:d09s1o$21v$05$(E-Mail Removed)-online.com...
> Gary Labowitz wrote:
>
> >> In my other function F, also with a void return type, can I do this?
> >>
> >> void F(int i)
> >> {
> >> // Can I return this?
> >> return ShowMessage("Hi Dean"); // ????
> >>
> >> }
> >>
> >> I can seem to be able to do this in Borland Builder6, but I didn't
> >> think it was possible until recently. Is this a compiler-specific
> >> setting?

> >
> > No, this doesn't make sense. It is invalid to return a value from a
> > function returning void.

>
> Nonsense! It is perfectly valid to do that, as long as the 'value' is of
> type void:
>
> void a()
> {
> }
>
> void b()
> {
> return a();
> }


Interesting.
If you coded b()'s return as
return void; //compiler error
But with the call to a() returning nothing, it looks like the 'nothing' is
the temporary value as a result of evaluating a(). On the other hand, the
compiler may be optimizing out the call to a( ) since a is empty. But no, if
there is code in the a( ) body it executes and so does the return.
void is surely tricky business, and the reason to allow void as a 'return'
from a function call makes sense.
There would appear to be no other way to set a temporary to void, I should
think, but if there is I'd like to see it.
--
Gary


 
Reply With Quote
 
Howard
Guest
Posts: n/a
 
      03-04-2005

"Gary Labowitz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> Interesting.
> If you coded b()'s return as
> return void; //compiler error


That's an error because "void" is a type, and you don't return types, you
return objects. It's like writing:
return int; // compiler error

> There would appear to be no other way to set a temporary to void, I should
> think, but if there is I'd like to see it.


I can't think of any way, either. I tried using something like
DoVoid(DoVoid()), and even DoVoid((void)(DoVoid())), but that doesn't
compile. (CodeWarrior just says the signatures don't match, but doesn't say
what it thinks the parameter is, if not a void temporary.)

-Howard


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      03-04-2005
Howard wrote:

>
> "Gary Labowitz" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>>
>> Interesting.
>> If you coded b()'s return as
>> return void; //compiler error

>
> That's an error because "void" is a type, and you don't return types, you
> return objects. It's like writing:
> return int; // compiler error


You can however do:

return (void)0;

Or (at least on g++):

return void();

>> There would appear to be no other way to set a temporary to void, I
>> should think, but if there is I'd like to see it.

>
> I can't think of any way, either. I tried using something like
> DoVoid(DoVoid()), and even DoVoid((void)(DoVoid())), but that doesn't
> compile. (CodeWarrior just says the signatures don't match, but doesn't
> say what it thinks the parameter is, if not a void temporary.)


g++ says "too many arguments", since the function expects zero argument, but
I provided one. Even though it's of type void, it is nevertheless an
argument. It would be a nice thing to have void parameters. This could be
useful in templates.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between void proba(); and void proba(void); ??? PencoOdStip@gmail.com C++ 1 05-23-2007 07:12 PM
CAN WE TYPE CAST AN INTEGER AS (VOID *)X..(Like can I return a value (void *)x) Abhishek C Programming 12 01-30-2006 03:22 PM
what is the difference, void func(void) and void fucn() noblesantosh@yahoo.com C Programming 5 07-22-2005 04:38 PM
"void Method()" vs "void Method(void)" Ollej Reemt C++ 7 04-22-2005 03:47 AM
`void **' revisited: void *pop(void **root) Stig Brautaset C Programming 15 10-28-2003 09:03 AM



Advertisments