| 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 | |
| 20 | struct A { |
| 21 | explicit A(int); |
| 22 | }; |
| 23 | // no implicit conversion |
| 24 | static_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 | |
| 27 | struct B { |
| 28 | B(int); |
| 29 | }; |
| 30 | // implicit conversion |
| 31 | static_assert(std::is_constructible_v<std::ranges::in_in_out_result<B, B, B>, std::ranges::in_in_out_result<int, int, int>>); |
| 32 | static_assert(std::is_constructible_v<std::ranges::in_in_out_result<B, B, B>, std::ranges::in_in_out_result<int, int, int>&>); |
| 33 | static_assert(std::is_constructible_v<std::ranges::in_in_out_result<B, B, B>, const std::ranges::in_in_out_result<int, int, int>>); |
| 34 | static_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 | |
| 36 | struct C { |
| 37 | C(int&); |
| 38 | }; |
| 39 | static_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& |
| 42 | static_assert(std::is_convertible_v<std::ranges::in_in_out_result<int, int, int>&, std::ranges::in_in_out_result<long, long, long>>); |
| 43 | static_assert(std::is_convertible_v<const std::ranges::in_in_out_result<int, int, int>&, std::ranges::in_in_out_result<long, long, long>>); |
| 44 | static_assert(std::is_convertible_v<std::ranges::in_in_out_result<int, int, int>&&, std::ranges::in_in_out_result<long, long, long>>); |
| 45 | static_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 |
| 48 | static_assert(std::is_move_constructible_v<std::ranges::in_in_out_result<MoveOnly, int, int>>); |
| 49 | static_assert(std::is_move_constructible_v<std::ranges::in_in_out_result<int, MoveOnly, int>>); |
| 50 | static_assert(std::is_move_constructible_v<std::ranges::in_in_out_result<int, int, MoveOnly>>); |
| 51 | |
| 52 | // should not be copy constructible |
| 53 | static_assert(!std::is_copy_constructible_v<std::ranges::in_in_out_result<MoveOnly, int, int>>); |
| 54 | static_assert(!std::is_copy_constructible_v<std::ranges::in_in_out_result<int, MoveOnly, int>>); |
| 55 | static_assert(!std::is_copy_constructible_v<std::ranges::in_in_out_result<int, int, MoveOnly>>); |
| 56 | |
| 57 | struct NotConvertible {}; |
| 58 | // conversions should not work if there is no conversion |
| 59 | static_assert(!std::is_convertible_v<std::ranges::in_in_out_result<NotConvertible, int, int>, std::ranges::in_in_out_result<int, int, int>>); |
| 60 | static_assert(!std::is_convertible_v<std::ranges::in_in_out_result<int, NotConvertible, int>, std::ranges::in_in_out_result<int, int, int>>); |
| 61 | static_assert(!std::is_convertible_v<std::ranges::in_in_out_result<int, int, NotConvertible>, std::ranges::in_in_out_result<int, int, int>>); |
| 62 | |
| 63 | template <class T> |
| 64 | struct ConvertibleFrom { |
| 65 | constexpr ConvertibleFrom(T c) : content{c} {} |
| 66 | T content; |
| 67 | }; |
| 68 | |
| 69 | constexpr 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 | |
| 101 | int main(int, char**) { |
| 102 | test(); |
| 103 | static_assert(test()); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |