Jump to content
  • Advertisement

Archived

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

capn_midnight

food for thought

This topic is 5516 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
Advertisement
This can be described by one word: hackish. If your function needs to return multiple data types then you need a new function.

Share this post


Link to post
Share on other sites
I guess it would work, but it''s pretty damn messy, and I can''t think of a good reason to actually use it...

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!