In C++, = default and {} are not the same constructor (opens external site)
View full thread · View on Hacker News
To get push notifications on iPhone or iPad, add Hack and Chat to your Home Screen: tap Share → Add to Home Screen, then open the installed app and enable push in Settings.
View full thread · View on Hacker News
Given that, it seems like a language could be designed so that a static method that returns an object could be allowed to construct an object in place. Again, I don't know enough about Rust or Zig to know if that's true of them.
Replies (3)
What happens inside
can be as "inefficient" as your example above, but the compiler would validate & figure out a way to make it an in-place-construction, guaranteed RVO.(similar to `constexpr` validation)
EDIT: Looks like this already exists under several names ("build-in-place", "placement by return", "result location semantics") in Ada's `return W : Widget do ... end return;`, Carbon's returned var c: Circle; ... return var;`, and Rust has (still open) RFC 2884 "placement by return", and Zig seems to have this fully built-in via result location semantics?!
Replies (1)
In c++ this is solved[1] via 1) emplace_back 2) perfect forwarding 3) constructors having an homogeneous name and being selected via overload resolution.
In a language with RVO, it is not enough for 'make_foo' in 'push_back(make_foo(args...))' to guarantee zero copy, 'push_back(x)' must also be able to guarantee that its parameter x is somehow constructed in place.
In practice you need some form of lazy evaluation (call by name, or an explicit thunk or passing the function and parameters separately) or some form of macro expansion.
[1] it does comes with its own set of foot-bazookas of course.
Ome can make it guaranteed (assuming Foo is an aggregate) by writing:
This guarantees initialization happens at the end of the function, since you are returning a pure rvalue. Generally a good style rule to adopt and no annotations needed.Replies (2)
(except they’re right and you just gained +1 to wisdom)
Replies (1)
https://en.cppreference.com/cpp/language/aggregate_initializ...
Generally, when you're doing the
pattern, it's because Foo is a struct with only public members and no constructor used for passing data around. e.g. When it's that simple rather than type out: You'd type: Congrats the code is shorter!Replies (1)