-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make
new
use list initialization if available
This enables uses like `new<std::array<int,5>>(1, 2, 3, 4, 5)` which didn't work before (not even with P0960) See #740 Note: I chose "use list init if available, else fall back to non-list init" instead of "use paren init if available, else fall back to list init" so that we would get consistent results for initializing a `new<std::vector<int>>` with `(10)`, `(10, 20)`, `(10, 20, 30)`, etc. (i.e., no surprise for `(10, 20)`)
- Loading branch information
Showing
2 changed files
with
15 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -556,14 +556,26 @@ auto Typeid() -> decltype(auto) { | |
struct { | ||
template<typename T> | ||
[[nodiscard]] auto cpp2_new(auto&& ...args) const -> std::unique_ptr<T> { | ||
return std::make_unique<T>(CPP2_FORWARD(args)...); | ||
if constexpr (requires { T{CPP2_FORWARD(args)...}; }) { | ||
// This is because apparently make_unique can't deal with list | ||
// initialization of aggregates, even after P0960 | ||
return std::unique_ptr<T>( new T{CPP2_FORWARD(args)...} ); | ||
} | ||
else { | ||
return std::make_unique<T>(CPP2_FORWARD(args)...); | ||
} | ||
} | ||
} inline unique; | ||
|
||
[[maybe_unused]] struct { | ||
template<typename T> | ||
[[nodiscard]] auto cpp2_new(auto&& ...args) const -> std::shared_ptr<T> { | ||
return std::make_shared<T>(CPP2_FORWARD(args)...); | ||
if constexpr (requires { T{CPP2_FORWARD(args)...}; }) { | ||
return unique.cpp2_new<T>(CPP2_FORWARD(args)...); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
hsutter
Author
Owner
|
||
} | ||
else { | ||
return std::make_shared<T>(CPP2_FORWARD(args)...); | ||
} | ||
} | ||
} inline shared; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32824 for x86 | ||
Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32825 for x86 | ||
Copyright (C) Microsoft Corporation. All rights reserved. | ||
|
Is it intentional that this call to
shared.cpp2_new
returns a call tounique.cpp2_new
?