Skip to content

Commit 1add5c5

Browse files
committed
Allow make_optional for T&, add test
1 parent 18c1b96 commit 1add5c5

File tree

3 files changed

+29
-47
lines changed

3 files changed

+29
-47
lines changed

libcxx/include/optional

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,9 +1437,13 @@ swap(optional<_Tp>& __x, optional<_Tp>& __y) noexcept(noexcept(__x.swap(__y))) {
14371437
__x.swap(__y);
14381438
}
14391439

1440+
struct __make_optional_barrier_tag {
1441+
explicit __make_optional_barrier_tag() = default;
1442+
};
1443+
14401444
template <
14411445
# if _LIBCPP_STD_VER >= 26
1442-
bool = false,
1446+
__make_optional_barrier_tag = __make_optional_barrier_tag{},
14431447
# endif
14441448
class _Tp>
14451449
_LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v) {
@@ -1448,17 +1452,11 @@ _LIBCPP_HIDE_FROM_ABI constexpr optional<decay_t<_Tp>> make_optional(_Tp&& __v)
14481452

14491453
template <class _Tp, class... _Args>
14501454
_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(_Args&&... __args) {
1451-
# if _LIBCPP_STD_VER >= 26
1452-
static_assert(!is_reference_v<_Tp>, "make_optional<T&, Args...> is disallowed");
1453-
# endif
14541455
return optional<_Tp>(in_place, std::forward<_Args>(__args)...);
14551456
}
14561457

14571458
template <class _Tp, class _Up, class... _Args>
14581459
_LIBCPP_HIDE_FROM_ABI constexpr optional<_Tp> make_optional(initializer_list<_Up> __il, _Args&&... __args) {
1459-
# if _LIBCPP_STD_VER >= 26
1460-
static_assert(!is_reference_v<_Tp>, "make_optional<T&, Args...> is disallowed");
1461-
# endif
14621460
return optional<_Tp>(in_place, __il, std::forward<_Args>(__args)...);
14631461
}
14641462

libcxx/test/std/utilities/optional/optional.specalg/make_optional_explicit.pass.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,26 @@
1919
#include <memory>
2020
#include <optional>
2121
#include <string>
22+
#include <string_view>
2223

2324
#include "test_macros.h"
2425

26+
template <typename T>
27+
constexpr bool test_ref() {
28+
T i{0};
29+
auto opt = std::make_optional<T&>(i);
30+
31+
#if TEST_STD_VER < 26
32+
assert((std::is_same_v<decltype(opt), std::optional<T>>));
33+
#else
34+
assert((std::is_same_v<decltype(opt), std::optional<T&>>));
35+
#endif
36+
37+
assert(*opt == 0);
38+
39+
return true;
40+
}
41+
2542
int main(int, char**)
2643
{
2744
{
@@ -43,6 +60,12 @@ int main(int, char**)
4360
auto opt = std::make_optional<std::string>(4u, 'X');
4461
assert(*opt == "XXXX");
4562
}
63+
using namespace std::string_view_literals;
64+
65+
static_assert(test_ref<int>());
66+
assert((test_ref<int>()));
67+
static_assert(test_ref<double>());
68+
assert((test_ref<double>()));
4669

47-
return 0;
70+
return 0;
4871
}

libcxx/test/std/utilities/optional/optional.specalg/make_optional_ref.verify.cpp

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)