logoalt Hacker News

kpw94 • today at 6:13 PM • 0 replies • view on HN

right, first-class support for a special say "must_construct_inplace" keyword on a function returning an object would be an interesting language design direction...

What happens inside

    must_construct_inplace Foo make_foo() { ... }
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?!