C++ shorthand "if then" issue

Started by
20 comments, last by Fruny 18 years, 1 month ago
Thanks for the clarification. I was mainly responding to the edit part of Gorax post.
Advertisement
There is, however, one exception:
throw statements are valid rvalues for the conditional operator
(or should be for most compilers anyway; I'm not certain on the official standing on this)
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
Quote:Original post by DrEvil
// Sometimes people nest them as well, in loop controls and such. It's pretty hideous but it works.
b ? c ? MyFunc1() : MyFunc2() : MyFunc3();


Yea. Code like that would make me kill the individual working with me. At the very least, he would find his IDE refused to load for the next few years... while he was in the same building as I was. :P

I find ? : to be convenient shorthand in a very few situations. The fact that C# 2.0 introduced the ?? operator makes me really irritable...

..what we do will echo throughout eternity..
What's ?? do in C# ?
Quote:Original post by DrEvil
What's ?? do in C# ?


a ?? b is equivalent to (a != null) ? a : b
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Damn, they actually deemed that worthy of its own operator? That's wierd. Thanks for the info.
Quote:Original post by DrEvil
Damn, they actually deemed that worthy of its own operator? That's wierd. Thanks for the info.


I believe GCC allows the use of a ?: b as an extension, to mean a ? a : b, which, I admit isn't quite the same thing (checks for truth rather than for null).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote:Original post by DrEvil
Damn, they actually deemed that worthy of its own operator? That's wierd. Thanks for the info.


I believe GCC allows the use of a ?: b as an extension, to mean a ? a : b.


Interesting. Does it evaluate a twice?
Not that I would recommend this for anything but fun, but I experimented with the conditional operator with multiple statements (output and assignment) and I had to do it like this:

((x%2)==0)?(cout<<"foo",y++):(cout<<"bar",z++);

This was in a for loop and the massive, ugly conditional above would test the sentinel and do one thing when it was even and the other when it was odd. Basically, it boils down to putting what you want it to do in parens, separating multiple statements with a comma.

This is very hairy code and I would wanna kill the author if I had to maintain it as part of any serious project, but it will work. Just use the regular if/else convention.
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.
Quote:Original post by Zahlman
Interesting. Does it evaluate a twice?


No.

Quote:Original post by Photonman

((x%2)==0)?(cout<<"foo",y++):(cout<<"bar",z++);



There is very little reason to ever use a conditional expression as a standalone statement.
cout << "Foo is " << (foo.enabled() ? "enabled" : "disabled") << endl; is more appropriate usage in my eyes.

Edit: Fixed precedence. See following post.

[Edited by - Fruny on March 15, 2006 1:54:15 PM]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement