Perl like syntax in c++
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?
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.
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.
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.
Yeah, I agree with you. The convieniance of it isn''t worth writing unusual looking syntax.
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!");
quote:Original post by SabreManTrue. I forgot that.quote:Original post by CWizard
#define or ||
That is already defined in ISO646.
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]
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
: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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement