3 ms·
In my C++ code, I've copied Rust and expressed fallible initialization by returning a std::optional<T> from a static method (which the article failed to mention
by nyanpasu64 5y ago
In my C++ code, I've copied Rust and expressed fallible initialization by returning a std::optional<T> from a static method (which the article failed to mention). The problem is that (like Rust) you lose placement initialization, and (unlike C++ or Rust) you can't initialize private fields using aggregate initialization or initializer lists, and must write a passthrough constructor (which can't even be private because it breaks make_unique).
https://github.com/hsutter/708 https://github.com/hsutter/708 is a C++ proposal which unifies placement constructors and writable out-parameters ("definite first use"). I don't think it makes placement initialization fallible, but I'm not sure.