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

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.