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>
12// struct in_in_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_result<A, A>, std::ranges::in_in_result<int, int>>);
25
26struct B {
27 B(int);
28};
29// implicit conversion
30static_assert(std::is_constructible_v<std::ranges::in_in_result<B, B>, std::ranges::in_in_result<int, int>>);
31static_assert(std::is_constructible_v<std::ranges::in_in_result<B, B>, std::ranges::in_in_result<int, int>&>);
32static_assert(std::is_constructible_v<std::ranges::in_in_result<B, B>, const std::ranges::in_in_result<int, int>>);
33static_assert(std::is_constructible_v<std::ranges::in_in_result<B, B>, const std::ranges::in_in_result<int, int>&>);
34
35struct C {
36 C(int&);
37};
38static_assert(!std::is_constructible_v<std::ranges::in_in_result<C, C>, std::ranges::in_in_result<int, int>&>);
39
40// has to be convertible via const&
41static_assert(std::is_convertible_v<std::ranges::in_in_result<int, int>&, std::ranges::in_in_result<long, long>>);
42static_assert(std::is_convertible_v<const std::ranges::in_in_result<int, int>&, std::ranges::in_in_result<long, long>>);
43static_assert(std::is_convertible_v<std::ranges::in_in_result<int, int>&&, std::ranges::in_in_result<long, long>>);
44static_assert(std::is_convertible_v<const std::ranges::in_in_result<int, int>&&, std::ranges::in_in_result<long, long>>);
45
46// should be move constructible
47static_assert(std::is_move_constructible_v<std::ranges::in_in_result<MoveOnly, int>>);
48static_assert(std::is_move_constructible_v<std::ranges::in_in_result<int, MoveOnly>>);
49
50struct NotConvertible {};
51// conversions should not work if there is no conversion
52static_assert(!std::is_convertible_v<std::ranges::in_in_result<NotConvertible, int>, std::ranges::in_in_result<int, int>>);
53static_assert(!std::is_convertible_v<std::ranges::in_in_result<int, NotConvertible>, std::ranges::in_in_result<int, int>>);
54
55template <class T>
56struct ConstructibleFrom {
57 constexpr ConstructibleFrom(T c) : content{c} {}
58 T content;
59};
60
61constexpr bool test() {
62 // Checks that conversion operations are correct.
63 {
64 std::ranges::in_in_result<int, double> res{10L, 0.};
65 assert(res.in1 == 10);
66 assert(res.in2 == 0.);
67 std::ranges::in_in_result<ConstructibleFrom<int>, ConstructibleFrom<double>> res2 = res;
68 assert(res2.in1.content == 10);
69 assert(res2.in2.content == 0.);
70 }
71
72 // Checks that conversions are possible when one of the types is move-only.
73 {
74 std::ranges::in_in_result<MoveOnly, int> res{MoveOnly{}, 0};
75 assert(res.in1.get() == 1);
76 [[maybe_unused]] auto res2 = static_cast<std::ranges::in_in_result<MoveOnly, int>>(std::move(res));
77 assert(res.in1.get() == 0);
78 }
79
80 // Checks that structured bindings get the correct values.
81 {
82 auto [in1, in2] = std::ranges::in_in_result<int, int>{1, 2};
83 assert(in1 == 1);
84 assert(in2 == 2);
85 }
86
87 return true;
88}
89
90int main(int, char**) {
91 test();
92 static_assert(test());
93
94 return 0;
95}
96

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