Get return value from function returning void
-
Hello.
I have the following function:
void f(int a) { return; }Then I trying to get its return value:
int x = f(10);Compiler says that it cannot convert
voidtoint.I tryed to do this way:
int x = (int)f(10);Still can't convert.
So, I decided to make variable
xthe same type as returning type off:void x = f(10);Still error. What I am doing wrong? I have learned that call of function is an expression, i.e. it has value which can be assigned somewhere.
-
Troll oder schlechter Witz?!

-
Nix Troll, fauler Typi, der sich nicht die Mühe macht, einen Post den er in ein englisches Forum gestellt hat ins Deutsche zu übersetzen.
Das Problem hat eine einfache Lösung:
void als return-Wert heißt halt mal "ich gebe nix zurück", da kannst du lange versuchen etwas zuzuweisen...
Wenn du was zurückgeben willst, musst du deine Funktion anpassen.
(Mannmannmann, seit 2007 ein C++Forum-Member....)
-
Trollhunter, really, not!
I am an old member here. I just trying to get deep knowledge of C++ and all its internals.
-
SAn schrieb:
Still error. What I am doing wrong? I have learned that call of function is an expression, i.e. it has value which can be assigned somewhere.
Yes, it is an expression. No, it doesn't have a value (at least none which can be assigned somewhere). You can't declare variables of type void. The only thing you can do with a void expression is return it from a void function:
void g(); void f() { return g(); }
-
SAn schrieb:
Trollhunter, really, not!
I am an old member here. I just trying to get deep knowledge of C++ and all its internals.
- this is a german C++-Forum. Aren't there any good english ones?
- void means nothing. returning void means returning nothing. And how one should apply nothing to an integer? and how sould "nothing" have a representation in memory?
What you often see in C-Apps is returning void-POINTERS; those can of course be casted, as a pointer has an adress.
-
Today I will give a lection to my students, and I need to clarify for them, what is the difference between operator, expression (also l-value and r-value). The problem is that function returning
voidseems to break this classification.P.S. Long time ago this forum was the top one in Google, so I decided to register. Sorry, I do not speak German.
-
@SAn: Just ignore the unregistered users. We still know you

-
SAn schrieb:
The problem is that function returning
voidseems to break this classification.I found it helpful to realize that void is an incomplete type, which, unlike a class type without a full definition, cannot be completed.
-
void is an incomplete type that cannot be completed. Therefore, no object of that type can exist. Consequently, no l-value expression of this type exists either.
A value of type void, however, does exist essentially meaning "nothing". There are (at least - hope I didnt forget one) 3 kinds of expressions of that type:
- a call to a function with the return type void
- a cast to type void, including the form void()
- throw expressions though these never actually do yield a value of void
All these expressions are r-value expressions which fits nicely into the system. An expression of type void may be the argument of a return statement in a void function (this is illegal in C btw), because there is no reason not to allow it, that helps to write generic code as well.A pointer of type cv void* is something different - this is a reuse of the keyword void roughly meaning "pointer to any type"
-
Bashar schrieb:
You can't declare variables of type void. The only thing you can do with a void expression is return it from a void function:
void g(); void f() { return g(); }Excellent! I can return it! So, call to a void function is indeed an expression.
Bashar schrieb:
I found it helpful to realize that void is an incomplete type, which, unlike a class type without a full definition, cannot be completed.
So, I cannot instantiate it (cannot make a variable of this type). Good explanation.
Also I have found the following: http://wiki.answers.com/Q/Why_can_you_not_declare_void_variables_in_c
It seems a little wrong to me, because I have checked:sizeof(void)==0. So, its looks likevoidtype has size (zero size).Also I have figured out the following
struct S { }; S f(int x) { S s; return s; } ... void; void; void; //Operator? Expression? s S = f(10);
-
camper schrieb:
void is an incomplete type that cannot be completed. Therefore, no object of that type can exist. Consequently, no l-value expression of this type exists either.
A value of type void, however, does exist essentially meaning "nothing". There are (at least - hope I didnt forget one) 3 kinds of expressions of that type:
- a call to a function with the return type void
- a cast to type void, including the form void()
- throw expressions though these never actually do yield a value of void
All these expressions are r-value expressions which fits nicely into the system. An expression of type void may be the argument of a return statement in a void function (this is illegal in C btw), because there is no reason not to allow it, that helps to write generic code as well.A pointer of type cv void* is something different - this is a reuse of the keyword void roughly meaning "pointer to any type"
So, as always, Camper came with complete explanation

Thank you!Added later:
void g(void) { return void(); return void(10); return (void)10; return static_cast<void>(10); }That is impressing me!
-
SAn schrieb:
It seems a little wrong to me, because I have checked: sizeof(void)==0.
That seems wrong
C++03 Standard schrieb:
5.3.3/1 ...
The sizeof operator shall not be applied to an expression that has function or incomplete type, or to an enumeration type before all its enumerators have been declared, or to the parenthesized name of such types, or to an lvalue that designates a bit-field.void is an incomplete type, so sizeof(void) is ill-formed.
This applies to C as well (6.5.3.4/1)Seems your compiler isn't conforming.
And
void;is ill-formed as well. Just like
int;would be.
-
Ok. Now I see that all these void things are rather consistent.
P.S. Using Visual Studio 2008 compiler.
-
SAn schrieb:
P.S. Using Visual Studio 2008 compiler.
void foo() { sizeof(void); }Visual C++ 2008 schrieb:
error C2070: 'void': illegal sizeof operand
what kind of code did you use?
-
I have checked
sizeof(void)during the debug session in the Watch window:
http://s49.radikal.ru/i124/0909/39/f6fb2218a7ae.png
I think Microsoft people forgot to fix this inconsistency
.