diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index 4210bdf8537..1eb888583cf 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -215,17 +215,17 @@ inline ErrorOr> adopt_nonnull_own_or_enomem(T* object) } template -requires(IsConstructible) inline OwnPtr try_make(Args&&... args) +requires(IsConstructible) inline ErrorOr> try_make(Args&&... args) { - return adopt_own_if_nonnull(new (nothrow) T(forward(args)...)); + return adopt_nonnull_own_or_enomem(new (nothrow) T(forward(args)...)); } // FIXME: Remove once P0960R3 is available in Clang. template -inline OwnPtr try_make(Args&&... args) +inline ErrorOr> try_make(Args&&... args) { - return adopt_own_if_nonnull(new (nothrow) T { forward(args)... }); + return adopt_nonnull_own_or_enomem(new (nothrow) T { forward(args)... }); } template diff --git a/Documentation/SmartPointers.md b/Documentation/SmartPointers.md index 9d63c2f335d..b9d617b6a02 100644 --- a/Documentation/SmartPointers.md +++ b/Documentation/SmartPointers.md @@ -33,13 +33,14 @@ There is a `make()` helper that constructs a new object and returns it wrappe } ``` -The `try_make()` helper attempts to construct a new object wrapped in an `OwnPtr`. All arguments passed to it are forwarded to `T`'s constructor. In case of allocation failure, a null pointer is returned. This allows the calling code to handle allocation failure as it wishes. +The `try_make()` helper attempts to construct a new object wrapped in an `ErrorOr>`. All arguments passed to it are forwarded to `T`'s constructor. In case of allocation failure, an ENOMEM error is returned. This allows the calling code to handle allocation failure as it wishes. ```cpp -OwnPtr my_object = try_make(); -if (!my_object) { +auto my_object_or_error = try_make(); +if (my_object_or_error.is_error()) { // handle allocation failure... } +auto my_object = my_object_or_error.release_value(); my_object->do_stuff(); ```