1 registered members (AndrewAMD),
1,534
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
return void in void function
#410424
11/02/12 22:25
11/02/12 22:25
|
Joined: Sep 2007
Posts: 101 Luxembourg
krial057
OP
Member
|
OP
Member
Joined: Sep 2007
Posts: 101
Luxembourg
|
Hi, I have the following problem. You might think, this has no purpose, but just read until the end. This is the code that i want to execute:
void foo2() {
return;
}
void foo() {
return foo2();
}
function main() {
foo();
}
However this doesn't work in lite-c (Can't convert GETRETV:::VOID) This works in other c-compilers and i could imagine that it is valid C code. Now I need a workaround for Lite-C... do you have any ideas. To make it clear why I need this. I have a macro that automatically creates some overloading of a function:
#define overload(returnType, overloadFunc) \
returnType overloadFunc(char* text, int style) \
{ \
return overloadFunc(text, NULL, style) \
} \
returnType overloadFunc(BMAP* img, int style) \
{ \
return overloadFunc(NULL, img, style) \
}
When i now have a function with void as return type, it won't work:
void drawTextOrImageWithStyle(char* text, BMAP* img, int style)
{
}
overload(void, drawTextOrImageWithStyle)
|
|
|
Re: return void in void function
[Re: krial057]
#410426
11/02/12 22:35
11/02/12 22:35
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
Expert
|
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
return doesn't work with voids. (void means that nothing gets returned) use function instead:
function foo2() {
return;
}
function foo() {
return foo2();
}
function main() {
foo();
}
Last edited by Kartoffel; 11/02/12 22:35.
POTATO-MAN saves the day! - Random
|
|
|
Re: return void in void function
[Re: Kartoffel]
#410427
11/02/12 23:35
11/02/12 23:35
|
Joined: Sep 2007
Posts: 101 Luxembourg
krial057
OP
Member
|
OP
Member
Joined: Sep 2007
Posts: 101
Luxembourg
|
@Kartoffel: Ty, i was also thinking about such a solution and I think i will use it(however i'm not too happy about it, because it may cause undefined behaviour if the users saves the result in an expression(this wouldn't be the case if void is used...) @MasterQ32: I don't agree that it makes no sense... You think so, because you never needed it xD I looked up if it is allowed in C99, and you are right, it isn't. However in c++ it is: An expression of type void shall be used only as an expression statement (6.2), as an operand of a comma expression (5.18), as a second or third operand of ?: (5.16), as the operand of typeid, or as the expression in a return statement (6.6.3) for a function with the return type void. In c++ it is useful for templates, so why shouldn't it be useful in c for macros? Anyway, thanks for your help
|
|
|
Re: return void in void function
[Re: krial057]
#410446
11/03/12 08:44
11/03/12 08:44
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
so then explain please what the function should return you can't do something like this (afaik):
void foo()
{
return (void)anystuff;
}
[...]
void myvar = foo();
so writing
void foo()
{
return foo2();
}
//should be equal to
void foo()
{
foo2();
return;
}
|
|
|
Re: return void in void function
[Re: MasterQ32]
#410463
11/03/12 12:08
11/03/12 12:08
|
Joined: Sep 2007
Posts: 101 Luxembourg
krial057
OP
Member
|
OP
Member
Joined: Sep 2007
Posts: 101
Luxembourg
|
I'm not sure about it... I think we need a C guru to clear it out. (I'm not sure at all about this, but I try to explain how I understand it...) For your 1st example:
void foo()
{
return (void)anystuff;
}
In my opinion, this should work. As explained in Wikipedia: The void type serves as a unit type So, why can't I return that unit type. For the second part of the code: This doesn't work, because: the void type is said to comprise an empty set of values, and the language does not provide any way to declare an object or represent a value with type void Your second example makes no sense for my use. I need to return the function value, if it is non-void... Here are also the results if compiled with gcc (no warnings or errors, but that has nothing to say...): http://ideone.com/BHcj3ohttp://ideone.com/KTO19U
|
|
|
Re: return void in void function
[Re: krial057]
#410465
11/03/12 13:50
11/03/12 13:50
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
i read the definition of void type in wikipedia, also the definition of unit type: A function with void result type ends either by reaching the end of the function or by executing a return statement with no returned value. The void type may also appear as the sole argument of a function prototype to indicate that the function takes no arguments. Note that despite the name, in all of these situations, the void type serves as a unit type, not as a zero or bottom type, even though unlike a real unit type which is a singleton, the void type is said to comprise an empty set of values, and the language does not provide any way to declare an object or represent a value with type void. In the area of mathematical logic, and computer science known as type theory, a unit type is a type that allows only one value (and thus can hold no information) following this both examples make no sense you are returning no information so why using return foo(); ?
|
|
|
Re: return void in void function
[Re: MasterQ32]
#410467
11/03/12 14:26
11/03/12 14:26
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
following this both examples make no sense you are returning no information so why using return foo(); ? He doesn't actually want to return anything, he just wants his macro to work with void as well without having an extra macro for void functions. Geez, am I the only one actually reading more than the thread title? Anyways, what does the C standard say to this? On page 47, section 6.3.2.2 you have the following statement about void: The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.)
(emphasis by me) So, if you cast something to void, the result of the cast will indeed be discarded, which is quite easy to test. The following run through Clang will generate no errors, and Krials GCC output back this up as well:
int test1()
{
printf("Hello World");
return 24;
}
void test2()
{
return (void)test1();
}
("Hello World" will be printed to stdout, confirming that the expression is indeed validated like the C standard mandates). Now, if this doesn't work in Lite-C: Bad luck for you, it should work.
Last edited by JustSid; 11/03/12 14:32.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: return void in void function
[Re: krial057]
#410469
11/03/12 14:56
11/03/12 14:56
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Well, you can have the dirty as fuck version which works because by exploiting three things: 1) Lite-C can't return structs on the stack 2) An empty return statement doesn't clear any registers 3) Emptry return statements are valid, even for functions returning something All you have to do is this:
int someTestFunction()
{
return 42;
}
int version1()
{
someTestFunct();
return;
}
void version2()
{
someTestFunct();
return;
}
The idea should be clear, one word of warning though: This is bound to break someday when the compiler gets updated!
Last edited by JustSid; 11/03/12 15:01.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
|