Archived

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

Perl like syntax in c++

This topic is 5144 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
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
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
quote:
Original post by Rayno
I thought
expression || exit(1);
would be easier to read than:
if( !expression ) exit(1);


You''re wrong. The de facto rule in C++ is that in order to maintain readability, short-circuiting should never be used with a right operand that has side-effects.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites