C++ shorthand "if then" issue
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)
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)
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...
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).
Quote:Original post by FrunyQuote: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.
((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.
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]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement