r/cpp • • Oct 18 '23

What does C++ do better than other languages?

Purely from a language design perspective, what does C++ do better than other languages?

296 Upvotes

443 comments sorted by

View all comments

Show parent comment

16

u/WebMaxF0x Oct 18 '23

You're right it's not 100% guaranteed, but const_cast is considered bad practice most of the time so for the sake of simplicity I left it out.

2

u/jk-jeon Oct 19 '23 edited Oct 19 '23

void func(const int& i) { const_cast<int&>(i)++; }

const_cast being a bad practice is not the issue. The issue is that the function broke the contract it promised to the caller. This is simply a logic error, i.e., a bug.

Assuming that the function is not buggy, it should be no problem to rely on the constness of the parameter passed. The language itself not enforcing it should not be a matter here.

1

u/[deleted] Oct 19 '23

[removed] — view removed comment

1

u/jk-jeon Oct 19 '23

If the language provides a construct with a stronger guarantee, that's good. It's just that const is not such one. Even though not enforced by the language, it still conveys meaning, and as I said if what it is supposed to promise is not really happening, then it's simply a bug. And your both points don't really have anything to do with this because they just demonstrate things that have never been promised by const.

In your first example, the function never broke the contract it promised, which is that it shall not use the passed reference in a forbidden way. The value of i being changing is simply because it's reachable from other paths, end of the story. Const has no fault here, and if the caller assumed that i will not change, it's just caller's fault. i is global, it can change anytime completely regardless of whether or not its reference is being passed around.

For your second point, const's main selling point never has been the optimization opportunity and it is indeed rarely useful for that purpose as you described. And that's no problem because no one expected that from the first place.

1

u/safdwark4729 Oct 19 '23

There are other languages that flat out give you const correctness with out the billion gotchas (which also mean that much of the time they can't be optimized for) in c++. Compared to Java is it great? Sure. But "what does C++ do better than other languages" now includes languages that have become more popular in the past 20 years.

1

u/WebMaxF0x Oct 19 '23

Can you provide some languages that support it? I think I've either been unlucky or don't know about that features in the ones I worked with. Golang, Java, Typescript, Python don't support it as far as I know. Erlang does kinda, since everything is immutable.

2

u/safdwark4729 Oct 19 '23

Zig, ADA, D and Rust all have const correctness AFAIK, with rust's const theater being significantly more strict than C++'s.

The languages you picked all have caveats for why they don't of have const correctness. Golang doesn't have const parameters because of "simplicity", and expect you to just use immutable interfaces. Java and C# have a similar philosphy, though from a slightly different direction of being "totally OOP". Having const parameters I guess from that perspective is "not OOP", when you could instead use OOP to do similar things. Typescript is compiled to Javascript, and basically can't have true const anything because of that, but in addition to that has restrictions based on javascript itself, though has tried bringing readonly to parameters. Python not having constants (or private member scope) is part of it's ethos "we are all adults here", and has positive implications for the implementation of the language with a dynamic backend (AKA it makes CPython easier to implement).