xref: /llvm-project/libcxx/test/std/algorithms/algorithms.results/in_out_out_result.pass.cpp (revision f0ea888e01aabcb131a8931b9e1fe1c5212a6cba)
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
10 
11 // template <class I1, class O1, class O2>
12 // struct in_out_out_result;
13 
14 #include <algorithm>
15 #include <cassert>
16 #include <type_traits>
17 
18 #include "MoveOnly.h"
19 
20 struct A {
21   explicit A(int);
22 };
23 // no implicit conversion
24 static_assert(!std::is_constructible_v<std::ranges::in_out_out_result<A, A, A>, std::ranges::in_out_out_result<int, int, int>>);
25 
26 struct B {
27   B(int);
28 };
29 // implicit conversion
30 static_assert(std::is_constructible_v<std::ranges::in_out_out_result<B, B, B>, std::ranges::in_out_out_result<int, int, int>>);
31 static_assert(std::is_constructible_v<std::ranges::in_out_out_result<B, B, B>, std::ranges::in_out_out_result<int, int, int>&>);
32 static_assert(std::is_constructible_v<std::ranges::in_out_out_result<B, B, B>, const std::ranges::in_out_out_result<int, int, int>>);
33 static_assert(std::is_constructible_v<std::ranges::in_out_out_result<B, B, B>, const std::ranges::in_out_out_result<int, int, int>&>);
34 
35 struct C {
36   C(int&);
37 };
38 static_assert(!std::is_constructible_v<std::ranges::in_out_out_result<C, C, C>, std::ranges::in_out_out_result<int, int, int>&>);
39 
40 // has to be convertible via const&
41 static_assert(std::is_convertible_v<std::ranges::in_out_out_result<int, int, int>&, std::ranges::in_out_out_result<long, long, long>>);
42 static_assert(std::is_convertible_v<const std::ranges::in_out_out_result<int, int, int>&, std::ranges::in_out_out_result<long, long, long>>);
43 static_assert(std::is_convertible_v<std::ranges::in_out_out_result<int, int, int>&&, std::ranges::in_out_out_result<long, long, long>>);
44 static_assert(std::is_convertible_v<const std::ranges::in_out_out_result<int, int, int>&&, std::ranges::in_out_out_result<long, long, long>>);
45 
46 // should be move constructible
47 static_assert(std::is_move_constructible_v<std::ranges::in_out_out_result<MoveOnly, int, int>>);
48 static_assert(std::is_move_constructible_v<std::ranges::in_out_out_result<int, MoveOnly, int>>);
49 static_assert(std::is_move_constructible_v<std::ranges::in_out_out_result<int, int, MoveOnly>>);
50 
51 static_assert(!std::is_copy_constructible_v<std::ranges::in_out_out_result<MoveOnly, int, int>>);
52 static_assert(!std::is_copy_constructible_v<std::ranges::in_out_out_result<int, MoveOnly, int>>);
53 static_assert(!std::is_copy_constructible_v<std::ranges::in_out_out_result<int, int, MoveOnly>>);
54 
55 struct NotConvertible {};
56 // conversions should not work if there is no conversion
57 static_assert(!std::is_convertible_v<std::ranges::in_out_out_result<NotConvertible, int, int>, std::ranges::in_out_out_result<int, int, int>>);
58 static_assert(!std::is_convertible_v<std::ranges::in_out_out_result<int, NotConvertible, int>, std::ranges::in_out_out_result<int, int, int>>);
59 static_assert(!std::is_convertible_v<std::ranges::in_out_out_result<int, int, NotConvertible>, std::ranges::in_out_out_result<int, int, int>>);
60 
61 template <class T>
62 struct ConvertibleFrom {
ConvertibleFromConvertibleFrom63   constexpr ConvertibleFrom(T c) : content{c} {}
64   T content;
65 };
66 
test()67 constexpr bool test() {
68   // Checks that conversion operations are correct.
69   {
70     std::ranges::in_out_out_result<int, double, float> res{10, 0., 1.f};
71     assert(res.in == 10);
72     assert(res.out1 == 0.);
73     assert(res.out2 == 1.f);
74     std::ranges::in_out_out_result<ConvertibleFrom<int>, ConvertibleFrom<double>, ConvertibleFrom<float>> res2 = res;
75     assert(res2.in.content == 10);
76     assert(res2.out1.content == 0.);
77     assert(res2.out2.content == 1.f);
78   }
79 
80   // Checks that conversions are possible when one of the types is move-only.
81   {
82     std::ranges::in_out_out_result<MoveOnly, int, int> res1{MoveOnly{}, 0, 0};
83     assert(res1.in.get() == 1);
84     auto res2 = static_cast<std::ranges::in_out_out_result<MoveOnly, int, int>>(std::move(res1));
85     assert(res1.in.get() == 0);
86     assert(res2.in.get() == 1);
87   }
88 
89   // Checks that structured bindings get the correct values.
90   {
91     auto [in, out1, out2] = std::ranges::in_out_out_result<int, int, int>{1, 2, 3};
92     assert(in == 1);
93     assert(out1 == 2);
94     assert(out2 == 3);
95   }
96   return true;
97 }
98 
main(int,char **)99 int main(int, char**) {
100   test();
101   static_assert(test());
102 
103   return 0;
104 }
105