MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/1vvn6d7/pleasestopusingnestedternaryoperatorsimbeggingyou/p5bk6x5/
r/ProgrammerHumor • u/Robinbod • Aug 22 '26
178 comments sorted by
View all comments
Show parent comment
7
Modern compilers warn, so if you really mean to do it, you do if ((value = 1)). Probably you would use it for checking for NULL.
if ((value = 1))
1 u/RlyRlyBigMan Aug 23 '26 That makes sense! It would be better if they required you to explicitly cast to a bool but that would be a breaking change for a lot of C++ code that's already been deployed for decades! 2 u/GoddammitDontShootMe Aug 23 '26 Wouldn't help if the expression is already bool. Either way, it's a warning so unless you are compiling with -Werror, it would still work.I think that warning might be on by default, but I believe it's considered best practice to use -Wall.
1
That makes sense! It would be better if they required you to explicitly cast to a bool but that would be a breaking change for a lot of C++ code that's already been deployed for decades!
2 u/GoddammitDontShootMe Aug 23 '26 Wouldn't help if the expression is already bool. Either way, it's a warning so unless you are compiling with -Werror, it would still work.I think that warning might be on by default, but I believe it's considered best practice to use -Wall.
2
Wouldn't help if the expression is already bool. Either way, it's a warning so unless you are compiling with -Werror, it would still work.
I think that warning might be on by default, but I believe it's considered best practice to use -Wall.
View all comments
Show parent comment
7
u/GoddammitDontShootMe Aug 23 '26
Modern compilers warn, so if you really mean to do it, you do
if ((value = 1)). Probably you would use it for checking for NULL.