Things that suck about C++ #1

29 comments · 12% upvoted · source · other discussions

comments

bitbait

Things that suck about C++ #1:

People who write blog posts about how bad C++ sucks for the last 15 years. Sorry but what does a reader gain from reading this??

Also that's not even C++ specific it's the same in C.

Ono-Sendai [OP]

Things that suck about C++ #2: You can't say anything bad about it because it is old apparently.

bitbait

It is like that in C ever since the ternary operator is around, so for the last 38 years. It has lower precedence than relational operators, logical operators, shifts etc.

How is it a reason that C++ sucks in 2016?

This operator and its precedence in C was around before most languages you're probably comparing it to even existed. I also totally fail to see how it's counter intuitive. It evaluates expressions. Clearly 10.0f + true is an expression so that's what's on the left side of the "?".

Ono-Sendai [OP]

If you read my post at all carefully you will see I'm not complaining about the precedence. I'm complaining about the type conversions (bool to float) resulting in lack of type safety. This was a problem 38 years ago and is still a problem.

Edit: serves me right for writing a blog post with any complexity, most people won't read it carefully.

_georgesim_

The fact that you think your post had any complexity explains why you had this bug to begin with. Most reasonable people just accept the fact the ternary operator has a lower precedence or don't write code this shitty to begin with.

Spartan-S63

Up vote for honesty. Pretty much the use of the ternary operator in the example is pretty poor and really hinders the readability of the code.

Ono-Sendai [OP]

You do realise that's a made-up example right? The actual source code is private.

Spartan-S63

If you're going to use a contrived example how is your criticism valid?

Ono-Sendai [OP]

It's a simplified (vars renamed, expression simplified a bit) version of a real bug.

Spartan-S63

Okay, so even if it's simplified, an inappropriate and convoluted use of the ternary operator was used. It's less a language issue and more of an issue of poor programming practices, in my opinion. The language enables it to happen but the programmer made a poor choice to use a feature like that that hinders readability.

Ono-Sendai [OP]

The post was sufficiently complex for people to misinterpret.

bitbait

If you read my post at all carefully you will see I'm not complaining about the precedence.

Of course you do and the fact that you think people don't "understand" what you're writing about or that you discovered something new or that something complex is happening here is sort of telling:

If you answered 11.f, you're wrong. The actual answer is 1.0f. The ternary conditional operator (?:) has very low precedence (See http://en.cppreference.com/w/cpp/language/operator_precedence, it's right down in box 15). The plus operator has higher precedence, so the expression gets evaluated as..

Then you go on and ramble about that this leads to what you call a "gross violation of any reasonable type system".

You can add 10.0f to a boolean value apparently and it's just fine!

Well of course you can. But again how would you think it's any different? Bool is not excluded from promotions. In C++ a boolean value is converted to int with value 0 if it's false and to int with value 1 if it's true. All you're doing there is adding an int to a float value which of course is promoted to a float. Everything what happens here are the basic rules of C and C++ and I believe everybody who learns the language for a couple of weeks can answer your question

First, pop quiz: What is the value of this C++ expression:

10.0f + true ? 1.0f : 2.0f;

And again I fail to see how this is a reason that C++ sucks which is worth mentioning in 2016 sry. I don't blame you for writing the post but I don't find it useful, interesting, curious or anything else.

edit: Actually the other posts on your blog deal with way more complicated topics, no idea why you have a problem here

spectre_theory

you're a moron

Ono-Sendai [OP]

fuck you.

spectre_theory

you want to write blog posts about programming? wash your mouth. do your parents know you're still up this late?

sQu1rr

How isn't that a common sense? What's next? are we gonna say that c++ is bad because 2 + 2 * 2 != 8? Learn precedence, where in doubt put parens. If you want to use these constructs compiler assumes you know what you are doing.

Ono-Sendai [OP]

Great attitude mate. Why don't we just remove all compiler warnings and error messages then? After all the compiler assumes you know what you're doing, and you wouldn't possibly write a bug would you?

sQu1rr

false-positive warnings for ?: will be too much and will pollute the output, static analysis is the way to go

Ono-Sendai [OP]

Clang warns about it actually.

shooshx

clang has this to say:
Warning:(368, 27) operator '?:' has lower precedence than '+'; '+' will be evaluated first"

so...

Things that suck about ignoring your compilation warnings #1

FTFY

Ono-Sendai [OP]

I don't ignore compilation warnings arsehole. That's how I found the bug.

Spartan-S63

What compiler did you use?

Ono-Sendai [OP]

I uses lots of different compilers. Clang is what found the bug.

Spartan-S63

I'm not sure I'd even use the ternary operator like that anyhow. That convolutes what the code is doing and really damages readability. I'd much rather have an if-else clause that makes the logic clear.

That said, operator precedence is a gotcha in every language, not just C++ and its weak type system is a trait of many other languages. So I'd say while this does suck, it's not really excessive to C++.

aurisc4

It's almost enough to know that multiplication & division takes precedence over add/subtract, pointer dereference is higher than multiplication. Just use parentheses for everything else and don't confuse people.

Bonus: ternary operator is confusing enough on it's own, using it inside expression is asking for trouble IHMO.

DrScabhands

We’ve been trying to reach you about your car’s extended warranty

spectre_theory

oh dear, someone is very upset.

this is scandalous!! scandalous i say!

pleasestandup

Things that suck about C++ : I never bothered to learn the basics of the language.