Skip to content

Commit e6a1cc3

Browse files
committed
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)`)
1 parent 8d66d57 commit e6a1cc3

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

include/cpp2util.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,26 @@ auto Typeid() -> decltype(auto) {
556556
struct {
557557
template<typename T>
558558
[[nodiscard]] auto cpp2_new(auto&& ...args) const -> std::unique_ptr<T> {
559-
return std::make_unique<T>(CPP2_FORWARD(args)...);
559+
if constexpr (requires { T{CPP2_FORWARD(args)...}; }) {
560+
// This is because apparently make_unique can't deal with list
561+
// initialization of aggregates, even after P0960
562+
return std::unique_ptr<T>( new T{CPP2_FORWARD(args)...} );
563+
}
564+
else {
565+
return std::make_unique<T>(CPP2_FORWARD(args)...);
566+
}
560567
}
561568
} inline unique;
562569

563570
[[maybe_unused]] struct {
564571
template<typename T>
565572
[[nodiscard]] auto cpp2_new(auto&& ...args) const -> std::shared_ptr<T> {
566-
return std::make_shared<T>(CPP2_FORWARD(args)...);
573+
if constexpr (requires { T{CPP2_FORWARD(args)...}; }) {
574+
return unique.cpp2_new<T>(CPP2_FORWARD(args)...);
575+
}
576+
else {
577+
return std::make_shared<T>(CPP2_FORWARD(args)...);
578+
}
567579
}
568580
} inline shared;
569581

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32824 for x86
1+
Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32825 for x86
22
Copyright (C) Microsoft Corporation. All rights reserved.
33

0 commit comments

Comments
 (0)