1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // template <class I, class T>
12 // struct in_value_result;
16 #include <type_traits>
24 // no implicit conversion
25 static_assert(!std::is_constructible_v
<std::ranges::in_value_result
<A
, A
>, std::ranges::in_value_result
<int, int>>);
30 // implicit conversion
31 static_assert(std::is_constructible_v
<std::ranges::in_value_result
<B
, B
>, std::ranges::in_value_result
<int, int>>);
32 static_assert(std::is_constructible_v
<std::ranges::in_value_result
<B
, B
>, std::ranges::in_value_result
<int, int>&>);
34 std::is_constructible_v
<std::ranges::in_value_result
<B
, B
>, const std::ranges::in_value_result
<int, int>>);
36 std::is_constructible_v
<std::ranges::in_value_result
<B
, B
>, const std::ranges::in_value_result
<int, int>&>);
41 static_assert(!std::is_constructible_v
<std::ranges::in_value_result
<C
, C
>, std::ranges::in_value_result
<int, int>&>);
43 // has to be convertible via const&
44 static_assert(std::is_convertible_v
<std::ranges::in_value_result
<int, int>&, std::ranges::in_value_result
<long, long>>);
46 std::is_convertible_v
<const std::ranges::in_value_result
<int, int>&, std::ranges::in_value_result
<long, long>>);
48 std::is_convertible_v
<std::ranges::in_value_result
<int, int>&&, std::ranges::in_value_result
<long, long>>);
50 std::is_convertible_v
<const std::ranges::in_value_result
<int, int>&&, std::ranges::in_value_result
<long, long>>);
52 // should be move constructible
53 static_assert(std::is_move_constructible_v
<std::ranges::in_value_result
<MoveOnly
, int>>);
54 static_assert(std::is_move_constructible_v
<std::ranges::in_value_result
<int, MoveOnly
>>);
56 // should not be copy constructible with move-only type
57 static_assert(!std::is_copy_constructible_v
<std::ranges::in_value_result
<MoveOnly
, int>>);
58 static_assert(!std::is_copy_constructible_v
<std::ranges::in_value_result
<int, MoveOnly
>>);
60 struct NotConvertible
{};
61 // conversions should not work if there is no conversion
63 !std::is_convertible_v
<std::ranges::in_value_result
<NotConvertible
, int>, std::ranges::in_value_result
<int, int>>);
65 !std::is_convertible_v
<std::ranges::in_value_result
<int, NotConvertible
>, std::ranges::in_value_result
<int, int>>);
68 struct ConvertibleFrom
{
69 constexpr ConvertibleFrom(T c
) : content
{c
} {}
73 constexpr bool test() {
74 // Checks that conversion operations are correct.
76 std::ranges::in_value_result
<int, double> res
{10, 0.};
78 assert(res
.value
== 0.);
79 std::ranges::in_value_result
<ConvertibleFrom
<int>, ConvertibleFrom
<double>> res2
= res
;
80 assert(res2
.in
.content
== 10);
81 assert(res2
.value
.content
== 0.);
84 // Checks that conversions are possible when one of the types is move-only.
86 std::ranges::in_value_result
<MoveOnly
, int> res
{MoveOnly
{}, 2};
87 assert(res
.in
.get() == 1);
88 assert(res
.value
== 2);
89 auto res2
= static_cast<std::ranges::in_value_result
<MoveOnly
, int>>(std::move(res
));
90 assert(res
.in
.get() == 0);
91 assert(res2
.in
.get() == 1);
92 assert(res2
.value
== 2);
95 // Checks that structured bindings get the correct values.
97 auto [in
, value
] = std::ranges::in_value_result
<int, int>{1, 2};
104 int main(int, char**) {
106 static_assert(test());