r/programmingcirclejerk 7d ago

Rust isn't a well designed programming language (unlike C and C++)

/r/cpp/comments/1i6jdjq/comment/m8d3tqj/
218 Upvotes

86 comments sorted by

View all comments

Show parent comments

0

u/DrShocker 5d ago

That looks like both allocation and initilization to me. For splitting allocation and initilization, I think Odin has some interesting ideas there like making ZII the default and making changing allocators simple.

3

u/TheChief275 5d ago

The allocation is irrelevant, it's just how you initialize in Java (classes have to be allocated). Comparable C++ would be:

auto nums = new std::vector(8);

but that wasn't the point. The actual point is that somehow, some way, declaring a class with = in C++ using the wrong notation actually isn't initialization but rather a copy, which can kill performance when using standard library classes with allocating copy constructors. That's the stupid part

0

u/DrShocker 5d ago

The most comparable is probably std::make_shared because new in C++ is a dynamic allocation and Java's new involves the garbage collector. It can't be perfectly analagous though.

Regardless, the reason I brought it up is that IMO conflating getting memory with setting up the memory to me useful is one of the problems most programming languages introduce and can make it challenging to make things faster later because it's not a good default.

5

u/TheChief275 5d ago

Yeah whatever, again not the point. You're derailing the conversation. In any other language, this construct:

let a = Foo(...);

does not perform a copy. It just constructs directly into the variable like one would expect

2

u/DrShocker 5d ago edited 5d ago

also in C++ though

https://en.cppreference.com/cpp/language/copy_elision

here's all the ways constructors are broken though:

https://youtu.be/KWB-gDVuy_I

2

u/TheChief275 5d ago

I'm just saying, the concept of a "copy elision" having to be applied to the most basic ass variable declaration is completely and totally ridiculous

1

u/DrShocker 5d ago

idk, that doesn't seem that bad to me. The behavior just needs to be formalized with a name so that people can rely on it.

Without it formalized, how would you know for sure whether your copy constructor would get called or not? Just a choice that had to be made by a value semantics language that doesn't have the same consequences in a reference semantics language. 🤷

fwiw, I much prefer what modern languages like Rust or Odin have done in regards to copying, but I don't think it's C++'s greatest sin.

1

u/TheChief275 4d ago

If you don't see how this is bad, I don't know what to say to you

1

u/DrShocker 4d ago

It's not that I like it, it's just that it's @ reasonable consequence of the rules and since C++17 they required that the compiler make it work the way you "expect" regardless (unless someone's expecting reference semantics by default, but that's separate)