Archived

This topic is now archived and is closed to further replies.

food for thought

This topic is 5151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, this isn''t a problem that I''m having (seems to be a lot of those these days), but a thought that popped into my head (imagine that! a thought, a real one). It''s totally crazy, and you may want to shoot me after I voice it, but I wanted to hear everyone''s opinions about it. Using C++ exceptions as a poly-type return system. for example:
void noRealReturn(int x){
switch(x){
case 0: throw ''A'';
case 1: throw 2;
default: throw 12.34;
}
}
 
in this way, I can return values of multiple data types, and use them in the subsequent catch statement, for a purpose other than error handling. So, what do you think? Completely insane? Probably, I''ve been up coding for the past 15 hours.

Share this post


Link to post
Share on other sites
Exceptions are to be used for error handling. They are not a control structure. Use something like boost::any to do what you want to do.

Also, exceptions are much much slower than return values.

[edited by - antareus on November 6, 2003 10:20:44 PM]

Share this post


Link to post
Share on other sites
There are some problems with this. One is that compilers optimize as much as they can for the case in which no exceptions are thrown. Two is that it routes around the type system. You still have to declare a return type for a function, but the return type is never used, and in fact the function can return any type. Third is that exceptions propagate upwards through the stack, so if you miss a type, it could be caught somewhere it wasn't meant to.

[edit]If you want to do this, use a language with a more flexible type system, such as Lisp, Python, or Haskell.

[edited by - Flarelocke on November 6, 2003 12:49:26 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by capn_midnight
...I''ve been up coding for the past 15 hours.

Coding what? (just wondering)

Share this post


Link to post
Share on other sites
I actually think it is kind of smart... despite it's hackish qualities... the only other ways I can think of for a function to return different data types are polymorphism or creating a struct specifically to hold the return value of this function

What I'm saying is that the legit ways of doing this are kind of messy looking anyway... so this could be like a cheap alternative to them...

Edit: I just checked out that Boost::any thing and that looks pretty cool too.

[edited by - tempuself on November 7, 2003 1:22:40 PM]

Share this post


Link to post
Share on other sites
Or, why not:


union returnUnion
{
char c;
int i;
float f;
};


returnUnion NoRealReturn(int x)
{
returnUnion r;

switch (x)
{
case 0:
r.c = 'A';
return r;
case 1:
r.i = 2;
return r;
default:
r.f = 12.34;
return r;
}
}

//...and the test in main...


returnUnion r;

r = NoRealReturn(0);
cout << r.c << endl;

r = NoRealReturn(1);
cout << r.i << endl;


r = NoRealReturn(10);
cout << r.f << endl;

Share this post


Link to post
Share on other sites
Using exceptions to handle value return is not standard coding and it may generate crashes itself!!!

You are using C++? Why not templates? Or something like this


class PolyType
{
public:

int data_type;
std::array<BYTE> data;
};

Share this post


Link to post
Share on other sites

>>I can return values of multiple data types
if you need this, then there is a big fault in the design;
what should be the advantage of this function/construction apart from your "great-multiple-return" thingy ???
there is no use for something like this.
apart from that: exceptions are for error handling - do you use a formula1car to drive on the farmland ? no.



DJSnow
---
this post is manually created and therefore legally valid without a signature

Share this post


Link to post
Share on other sites
There is use for variant return types; especially when coupling a loosely typed system with a strongly typed language like C++.

Consider the case of getting a named value out of some script or entity. If your script/entity system is loosely typed, then the value of the property "Special" may be 1, or 3.14, or "foo+bar".

However, even if you manage to propagate multiple types out of a function using exceptions, which is bound to be very slow, then what? You''re likely to want to keep propagating these values further, which leads to the hack taking over your code base. Bad mojo.

Instead, use some kind of variant type that uses either good-old runtime polymorphism, or even gooder-older if()/switch() to deal with multiple types.

Share this post


Link to post
Share on other sites