An unofficial, read-only Hacker News reader. Join the beta →

In C++, = default and {} are not the same constructor (opens external site)

129 points by surprisetalk 2 days ago | lemire.me | 198 comments on HN

View full thread · View on Hacker News

Comment thread (2)

  1. I think you can't get rid of constructors until you either get rid of object identity, or get rid of encapsulation. C never had encapsulation as a language feature, so it didn't need constructors, while Rust eliminated object identity - a value can be arbitrarily moved from one variable to another and there's no (safe) way to notice that happened.

    But in C++ one variable is one object. When you write "mystruct foo;" a new mystruct is created at that point. That is a specific one that has a specific memory address and is not interchangeable with any other. If you wrote "mystruct foo = create_mystruct();" that would be kicking the can down the road because the mystruct that is returned by create_mystruct is not the same one as the variable foo, so you still need a constructor to initialize foo based on the result of create_mystruct before the result mystruct is destroyed. You could define it to be a bitwise copy but that wouldn't work for a lot of types (like string, or shared_ptr).

    In a language like C, you define it as a bitwise copy and anything that becomes incorrect after a bitwise copy is left as an exercise to the programmer. In a language like Rust you have a strong ownership model that permits a bitwise move - which is the same as a copy, but the original is no longer considered an object, and won't have its destructor called, and the destination wasn't an object until it was copied into. This requires that the object doesn't hold a pointer to itself. In C++ you have neither and you have be able to call user-defined code here.

    Later versions of C++ have something called RVO which actually allows the copy to be omitted but only in the specific case above. Other cases still need copies. The language is tortured to say that create_mystruct's return value isn't actually a separate temporary instance of mystruct but whatever goes in the return expression of create_mystruct is actually used to construct foo in the caller. Compilers implement this by secretly adding a parameter to hold the address of where the return object should be constructed.

    Replies (1)
    1. Excellent explanation, thanks. Rust’s move semantics around “all types must survive a memcpy” are (IMO) a great idea 99.9% of the time (no move constructors, no worrying about “pillaging” the source of a move to make its destructor safe to run, you can express more invariants since you don’t need to be able to express such pillaging) but it’s one of the places where something that is “simple” in C++ is truly hard to accomplish in Rust.

      Rust has added Pin later on in the language to express data that can’t be moved, but IME I only see it used in desugared async code to guarantee that values stay put between awaits, and it’s not common to see Pin used for general code. I don't think you can just sprinkle some Pin<T> into existing code and solve the object identity problem, although I've never really had to deal with code that's move-sensitive so I don't know.