struct Foo {};
// Legacy C-like API for creating an object Foo.
bool TryCreateFoo(Foo** out);
void BeforeCpp23() {
Foo* foo_raw = nullptr;
if (!TryCreateFoo(&foo_raw)) {
return;
}
std::unique_ptr<Foo> foo(foo_raw);
// …
}
void AfterCpp23() {
std::unique_ptr<Foo> foo;
// Sets `foo` directly without requiring an extra var.
if (!TryCreateFoo(std::out_ptr(foo))) {
return;
}
// …
}
```if (!TryCreateFoo(std::out_ptr(foo)) || !foo) {
return;
}
would not behave as the author expected, and I'm wondering if it's worth any kind of guidance or checks for this case.
--
You received this message because you are subscribed to the Google Groups "cxx" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cxx+uns...@chromium.org.
To view this discussion visit https://groups.google.com/a/chromium.org/d/msgid/cxx/CANFyMwzEJYSOqgSqPWAxFP12_W68XV0h42x%2BB4P%2BbUZHrp4rWw%40mail.gmail.com.
It is not recommended to create an out_ptr_t object of a storage duration other than automatic storage duration, because such code is likely to produce dangling references and result in undefined behavior on destruction.