xref: /llvm-project/libcxx/test/std/algorithms/algorithms.results/in_value_result.pass.cpp (revision 3903438860b6eebf53a912c672560e1e55311220)
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 
11 // template <class I, class T>
12 // struct in_value_result;
13 
14 #include <algorithm>
15 #include <cassert>
16 #include <type_traits>
17 #include <utility>
18 
19 #include "MoveOnly.h"
20 
21 struct A {
22   explicit A(int);
23 };
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>>);
26 
27 struct B {
28   B(int);
29 };
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>&>);
33 static_assert(
34     std::is_constructible_v<std::ranges::in_value_result<B, B>, const std::ranges::in_value_result<int, int>>);
35 static_assert(
36     std::is_constructible_v<std::ranges::in_value_result<B, B>, const std::ranges::in_value_result<int, int>&>);
37 
38 struct C {
39   C(int&);
40 };
41 static_assert(!std::is_constructible_v<std::ranges::in_value_result<C, C>, std::ranges::in_value_result<int, int>&>);
42 
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>>);
45 static_assert(
46     std::is_convertible_v<const std::ranges::in_value_result<int, int>&, std::ranges::in_value_result<long, long>>);
47 static_assert(
48     std::is_convertible_v<std::ranges::in_value_result<int, int>&&, std::ranges::in_value_result<long, long>>);
49 static_assert(
50     std::is_convertible_v<const std::ranges::in_value_result<int, int>&&, std::ranges::in_value_result<long, long>>);
51 
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>>);
55 
56 // should not 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>>);
59 
60 struct NotConvertible {};
61 // conversions should not work if there is no conversion
62 static_assert(
63     !std::is_convertible_v<std::ranges::in_value_result<NotConvertible, int>, std::ranges::in_value_result<int, int>>);
64 static_assert(
65     !std::is_convertible_v<std::ranges::in_value_result<int, NotConvertible>, std::ranges::in_value_result<int, int>>);
66 
67 template <class T>
68 struct ConvertibleFrom {
69   constexpr ConvertibleFrom(T c) : content{c} {}
70   T content;
71 };
72 
73 constexpr bool test() {
74   {
75     std::ranges::in_value_result<int, double> res{10, 0.};
76     assert(res.in == 10);
77     assert(res.value == 0.);
78     std::ranges::in_value_result<ConvertibleFrom<int>, ConvertibleFrom<double>> res2 = res;
79     assert(res2.in.content == 10);
80     assert(res2.value.content == 0.);
81   }
82   {
83     std::ranges::in_value_result<MoveOnly, int> res{MoveOnly{}, 2};
84     assert(res.in.get() == 1);
85     assert(res.value == 2);
86     auto res2 = static_cast<std::ranges::in_value_result<MoveOnly, int>>(std::move(res));
87     assert(res.in.get() == 0);
88     assert(res2.in.get() == 1);
89     assert(res2.value == 2);
90   }
91   {
92     auto [in, value] = std::ranges::in_value_result<int, int>{1, 2};
93     assert(in == 1);
94     assert(value == 2);
95   }
96   return true;
97 }
98 
99 int main(int, char**) {
100   test();
101   static_assert(test());
102 
103   return 0;
104 }
105