1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// There is a bug in older versions of Clang that causes trouble with constraints in classes like
// `ContainerWithDirectCtr`.
// XFAIL: apple-clang-15
// template<template<class...> class C, input_range R, class... Args>
// constexpr auto to(R&& r, Args&&... args); // Since C++23
#include <ranges>
#include <algorithm>
#include <array>
#include <cassert>
#include "container.h"
template <class ElementType>
struct ContainerWithDirectCtr : Container<ElementType, CtrChoice::DirectCtr> {
using Container<ElementType, CtrChoice::DirectCtr>::Container;
};
template <std::ranges::input_range Range>
ContainerWithDirectCtr(Range&&) -> ContainerWithDirectCtr<std::ranges::range_value_t<Range>>;
template <std::ranges::input_range Range>
ContainerWithDirectCtr(Range&&, int, char) -> ContainerWithDirectCtr<std::ranges::range_value_t<Range>>;
template <class ElementType>
struct ContainerWithFromRangeT : Container<ElementType, CtrChoice::FromRangeT> {
using Container<ElementType, CtrChoice::FromRangeT>::Container;
};
template <std::ranges::input_range Range>
ContainerWithFromRangeT(std::from_range_t, Range&&) -> ContainerWithFromRangeT<std::ranges::range_value_t<Range>>;
template <std::ranges::input_range Range>
ContainerWithFromRangeT(std::from_range_t, Range&&, int, char) ->
ContainerWithFromRangeT<std::ranges::range_value_t<Range>>;
template <class ElementType>
struct ContainerWithBeginEndPair : Container<ElementType, CtrChoice::BeginEndPair> {
using Container<ElementType, CtrChoice::BeginEndPair>::Container;
};
template <class Iter>
ContainerWithBeginEndPair(Iter, Iter) -> ContainerWithBeginEndPair<std::iter_value_t<Iter>>;
template <class Iter>
ContainerWithBeginEndPair(Iter, Iter, int, char) -> ContainerWithBeginEndPair<std::iter_value_t<Iter>>;
constexpr bool test() {
std::array in = {1, 2, 3, 4, 5};
int arg1 = 42;
char arg2 = 'a';
{ // Case 1 -- can construct directly from the given range.
{
std::same_as<ContainerWithDirectCtr<int>> decltype(auto) result = std::ranges::to<ContainerWithDirectCtr>(in);
assert(result.ctr_choice == CtrChoice::DirectCtr);
assert(std::ranges::equal(result, in));
assert((in | std::ranges::to<ContainerWithDirectCtr>()) == result);
}
{ // Extra arguments.
std::same_as<ContainerWithDirectCtr<int>> decltype(auto) result =
std::ranges::to<ContainerWithDirectCtr>(in, arg1, arg2);
assert(result.ctr_choice == CtrChoice::DirectCtr);
assert(std::ranges::equal(result, in));
assert(result.extra_arg1 == arg1);
assert(result.extra_arg2 == arg2);
assert((in | std::ranges::to<ContainerWithDirectCtr>(arg1, arg2)) == result);
}
}
{ // Case 2 -- can construct from the given range using the `from_range_t` tagged constructor.
{
std::same_as<ContainerWithFromRangeT<int>> decltype(auto) result = std::ranges::to<ContainerWithFromRangeT>(in);
assert(result.ctr_choice == CtrChoice::FromRangeT);
assert(std::ranges::equal(result, in));
assert((in | std::ranges::to<ContainerWithFromRangeT>()) == result);
}
{ // Extra arguments.
std::same_as<ContainerWithFromRangeT<int>> decltype(auto) result =
std::ranges::to<ContainerWithFromRangeT>(in, arg1, arg2);
assert(result.ctr_choice == CtrChoice::FromRangeT);
assert(std::ranges::equal(result, in));
assert(result.extra_arg1 == arg1);
assert(result.extra_arg2 == arg2);
assert((in | std::ranges::to<ContainerWithFromRangeT>(arg1, arg2)) == result);
}
}
{ // Case 3 -- can construct from a begin-end iterator pair.
{
std::same_as<ContainerWithBeginEndPair<int>> decltype(auto) result =
std::ranges::to<ContainerWithBeginEndPair>(in);
assert(result.ctr_choice == CtrChoice::BeginEndPair);
assert(std::ranges::equal(result, in));
assert((in | std::ranges::to<ContainerWithBeginEndPair>()) == result);
}
{ // Extra arguments.
std::same_as<ContainerWithBeginEndPair<int>> decltype(auto) result =
std::ranges::to<ContainerWithBeginEndPair>(in, arg1, arg2);
assert(result.ctr_choice == CtrChoice::BeginEndPair);
assert(std::ranges::equal(result, in));
assert(result.extra_arg1 == arg1);
assert(result.extra_arg2 == arg2);
assert((in | std::ranges::to<ContainerWithBeginEndPair>(arg1, arg2)) == result);
}
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|