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 136 137 138
|
//===----------------------------------------------------------------------===//
//
// 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
// owning_view() requires default_initializable<R> = default;
// constexpr owning_view(R&& t);
#include <ranges>
#include <cassert>
#include <concepts>
#include <type_traits>
#include <utility>
#include "test_macros.h"
struct DefaultConstructible {
int i;
constexpr explicit DefaultConstructible(int j = 42) : i(j) {}
int *begin() const;
int *end() const;
};
struct NotDefaultConstructible {
int i;
constexpr explicit NotDefaultConstructible(int j) : i(j) {}
int *begin() const;
int *end() const;
};
struct MoveChecker {
int i;
constexpr explicit MoveChecker(int j) : i(j) {}
constexpr MoveChecker(MoveChecker&& v) : i(std::exchange(v.i, -1)) {}
MoveChecker& operator=(MoveChecker&&);
int *begin() const;
int *end() const;
};
struct NoexceptChecker {
int *begin() const;
int *end() const;
};
constexpr bool test()
{
{
using OwningView = std::ranges::owning_view<DefaultConstructible>;
static_assert(std::is_constructible_v<OwningView>);
static_assert(std::default_initializable<OwningView>);
static_assert(std::movable<OwningView>);
static_assert(std::is_trivially_move_constructible_v<OwningView>);
static_assert(std::is_trivially_move_assignable_v<OwningView>);
static_assert(!std::is_copy_constructible_v<OwningView>);
static_assert(!std::is_copy_assignable_v<OwningView>);
static_assert(!std::is_constructible_v<OwningView, int>);
static_assert(!std::is_constructible_v<OwningView, DefaultConstructible&>);
static_assert(std::is_constructible_v<OwningView, DefaultConstructible&&>);
static_assert(!std::is_convertible_v<int, OwningView>);
static_assert(std::is_convertible_v<DefaultConstructible&&, OwningView>);
{
OwningView ov;
assert(ov.base().i == 42);
}
{
OwningView ov = OwningView(DefaultConstructible(1));
assert(ov.base().i == 1);
}
}
{
using OwningView = std::ranges::owning_view<NotDefaultConstructible>;
static_assert(!std::is_constructible_v<OwningView>);
static_assert(!std::default_initializable<OwningView>);
static_assert(std::movable<OwningView>);
static_assert(std::is_trivially_move_constructible_v<OwningView>);
static_assert(std::is_trivially_move_assignable_v<OwningView>);
static_assert(!std::is_copy_constructible_v<OwningView>);
static_assert(!std::is_copy_assignable_v<OwningView>);
static_assert(!std::is_constructible_v<OwningView, int>);
static_assert(!std::is_constructible_v<OwningView, NotDefaultConstructible&>);
static_assert(std::is_constructible_v<OwningView, NotDefaultConstructible&&>);
static_assert(!std::is_convertible_v<int, OwningView>);
static_assert(std::is_convertible_v<NotDefaultConstructible&&, OwningView>);
{
OwningView ov = OwningView(NotDefaultConstructible(1));
assert(ov.base().i == 1);
}
}
{
using OwningView = std::ranges::owning_view<MoveChecker>;
static_assert(!std::is_constructible_v<OwningView>);
static_assert(!std::default_initializable<OwningView>);
static_assert(std::movable<OwningView>);
static_assert(!std::is_trivially_move_constructible_v<OwningView>);
static_assert(!std::is_trivially_move_assignable_v<OwningView>);
static_assert(!std::is_copy_constructible_v<OwningView>);
static_assert(!std::is_copy_assignable_v<OwningView>);
static_assert(!std::is_constructible_v<OwningView, int>);
static_assert(!std::is_constructible_v<OwningView, MoveChecker&>);
static_assert(std::is_constructible_v<OwningView, MoveChecker&&>);
static_assert(!std::is_convertible_v<int, OwningView>);
static_assert(std::is_convertible_v<MoveChecker&&, OwningView>);
{
// Check that the constructor does indeed move from the target object.
auto m = MoveChecker(42);
OwningView ov = OwningView(std::move(m));
assert(ov.base().i == 42);
assert(m.i == -1);
}
}
{
// Check that the defaulted constructors are (not) noexcept when appropriate.
static_assert( std::is_nothrow_constructible_v<NoexceptChecker>); // therefore,
static_assert( std::is_nothrow_constructible_v<std::ranges::owning_view<NoexceptChecker>>);
static_assert(!std::is_nothrow_constructible_v<DefaultConstructible>); // therefore,
static_assert(!std::is_nothrow_constructible_v<std::ranges::owning_view<DefaultConstructible>>);
static_assert( std::is_nothrow_move_constructible_v<NoexceptChecker>); // therefore,
static_assert( std::is_nothrow_move_constructible_v<std::ranges::owning_view<NoexceptChecker>>);
static_assert(!std::is_nothrow_move_constructible_v<MoveChecker>); // therefore,
static_assert(!std::is_nothrow_move_constructible_v<std::ranges::owning_view<MoveChecker>>);
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
|