Jump to content
  • Advertisement

Archived

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

Rayno

Perl like syntax in c++

This topic is 5433 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

In perl stuff like this is pretty common: boolean expression or statement eg: open( FILE, "file.txt" ) or die "Failed to open file"; My question is wether this is a good idea to use in c/c++. I ask this because I haven''t seen it used before (in C/C++), even though it does work. I wasn''t sure if it would but I tried it on two compilers (VC++''03, and Mingw), and it works as expected. Is something like (obviusly a trivial expample, but you get the point): Flag || cout << "Flag is false\n"; (1)standard compilient C or C++, and (2) a Good Thing to use?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
The problem is that it doesn''t work that often.

void foo() {}
<expression> || foo(); //won''t compile

foo() doesn''t have a return value so it can''t be used inside an expression. So because it could be used only in cases where something is returned, it isn''t adviced to use that idiom in C++ at all IMO. Too inconsistent.

Share this post


Link to post
Share on other sites
Oh, I see. I didn''t think of that.
Yeah, I agree with you. The convieniance of it isn''t worth writing unusual looking syntax.

Share this post


Link to post
Share on other sites
I wonder if this would work:

#define or ||
#define die(x) (std::cout << x, exit(-1))

strlen(some_string) or die("Give me a string!");

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
quote:
Original post by CWizard
#define or ||

That is already defined in ISO646.
True. I forgot that.

Share this post


Link to post
Share on other sites
quote:
Original post by Arild Fines
As if C++ syntax isn't bad enough already...


I thought
expression || exit(1);
would be easier to read than:
if( !expression ) exit(1);

I'm not completely convinced of it's uselessness.

By the way, you could also do the equivalent of:
if( expression ) cout << "True!\n";
like:
expression && cout << "True!\n";


[edited by - rayno on November 11, 2003 3:23:19 PM]

Share this post


Link to post
Share on other sites
i have exceptions.. why do i need or die?

:D




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!