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

source code of libcxx/test/std/algorithms/algorithms.results/in_in_out_result.pass.cpp