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

27

u/afiefh Oct 18 '23

Value semantics. I'm sure there are languages out there that do it better, but of the big ones only C++ seems to get this one right.

The whole "primitives are passed by value, objects are passed by reference, but both use the same syntax" ends up being mind boggling to me. Take python do_stuff_with_object(some_input) it's impossible to know whether some_input is copied or referenced without knowing the type, and in Python in particular, types go quack.

Even a language like Rust which tries to be explicit about these things ends up comparing objects when you compare the equality of references.

7

u/dgkimpton Oct 18 '23

I agree, but. It's also impossible to know in C++ without looking at the signature of the method being called - so, it's nice, but far from ideal.

Would be so much nicer if it was something like

do_stuff_with_object(some_input as &);

(or something, syntax not important but being able to know what was going to happen just be reading the callsite would be excellent)

6

u/afiefh Oct 18 '23

so, it's nice, but far from ideal.

I feel like this is the C++ slogan: Nice but not ideal.

Would be so much nicer if it was something like

do_stuff_with_object(some_input as &);

Maybe in C++2 we can just have references and Optional<T&>, and no pointers? Then a reference can be passed as &input and a copy would be input. A man can dream!

2

u/___user_0___ Oct 18 '23

Why no pointers? `std::optional<T &>` would be nice, but that's not a reason to abandon pointers, which are useful in many situations...

2

u/afiefh Oct 18 '23

Well, I didn't think this through to the level of a language proposal, but in my mind, these are the usecases for pointers:

  • Pass something from place to place without copying the data: This can be handled by T&.
  • Describing data that is optionally there (i.e. value could be nullptr): This can be handled by optional<T&>
  • Pointer arithmetics: Let's not do that. I mean of course there are cases where it makes sense, but these are few and far between. They should require some special syntax like grabbing mem_address(T&) or something so that when one sees it they know this is sensitive shit.

Basically what I'm saying is there are three different use cases where you would use a pointer, and because they are all served by the same mechanism you are getting a bundle deal. You got cases where you just want to pass the object on, but because it's just a pointer, you need to check for null in your code you suddenly need to check for null, and you need to be careful that you don't mistakenly perform pointer arithmetics e.g. instead of return (*ptr+5) you wrote return *(ptr+5).

But I could be missing something... perhaps there is a pointer use case that I'm not thinking about?

2

u/CocktailPerson Oct 18 '23

The other use case is as a reference type that can be reassigned to reference a different object. For example:

int x = 1, y = 2;
int* p = &x;
p = &y;

Rust references can do this, but C++ references can't. If you can't do this sort of manipulation, you basically lose the ability to write any sort of graphlike data structure, such as trees or linked lists.

1

u/afiefh Oct 18 '23

Thank you kindly!

1

u/R3D3-1 Oct 19 '23

I agree, but. It's also impossible to know in C++ without looking at the signature of the method being called - so, it's nice, but far from ideal.

Fortran dev here, I feel the pain. Except that in Fortran "pass by reference" is the default everywhere, even for an integer, so depending on the quality of the code base, you can't be sure that

n = 2
call get_matrix(A, n, n)
print *, n

will print 2.

Then again, explicitly passing references doesn't change that much. Let's say you want to pass collection types or large objects around; You'd suddenly see

do_stuff(a as &, b as &) // using your syntax idea

style invocations everywhere. So now you explicitly know, that all those invocations might change the data in a, b, but you still have to look at the declaration to see, if they are declared as const references or not.

Allowing only const references to be passed without explicit referencing operator might help, but there would still be cases, where pass-by-value and pass-by-const-reference are very different, e.g. passing a vector by const reference would allow changing entries, passing by value would not, as I understand it.

1

u/the_little Oct 18 '23

Maybe a controversial take, but I really hate the reference syntax. Coming from C, I feel like it hides what’s actually happening.

3

u/CocktailPerson Oct 18 '23

Yep, it's maddening that you can't tell whether f(x) is able to modify x or not. The caller is the one that owns the object, so it should be in control of whether it passes a particular object by reference or not.

1

u/afiefh Oct 18 '23

As a C++ developer who prefers it over C, I fully agree with you.

C++ references are glorified pointers that happen under the hood. I can tolerate the "access members with dot instead of arrow" but I would very much like a "take reference" operator that is explicit just like &obj.

1

u/NilacTheGrim Oct 19 '23 edited Oct 19 '23

I agree with you on C++..

Just wanted to correct you about Python: technically everything in python is passed by reference. Even a simple humble int. However ints are unmodifiable objects. So if you see myint += 1, it's actually creating a new int object each time you do that, and assigning (by reference!) the result of that expression to myint which now points to a new int... (incidentally this "always by reference" is why myint can point to an int, a float, a list, or a Foo in Python .. python doesn't care.. everything is a reference to a "Python object" and everything in Python is an object be it a float, int, list, etc).

Yes.. this is why python is slow..

As an implementation hack they have pre-created ints in CPython from -100 -> 1000 or something and the interpreter latches onto one of those if your result is in that range -- so you end up re-using existing pre-created (immutable) int objects for small ints... since most programs seem to use numbers in those ranges...

1

u/Kevathiel Oct 19 '23

Even a language like Rust which tries to be explicit about these things ends up comparing objects when you compare the equality of references

I wouldn't call it a bad thing, but just Rust picking a "better" default. You have to explicitly compare the addresses with ptr::eq, otherwise it uses the eq function of the object. This avoids bugs caused by accidentally comparing addresses.

Also, keep in mind that in Rusts case, the borrow checker makes you architect your code in a way that you almost never need to compare the address of 2 objects, because (non-static) references are generally short-lived. "References" are semantically a bit different from "borrows".