88 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++14, c++1710 11// constexpr V base() const & requires copy_constructible<V> { return base_; }12// constexpr V base() && { return std::move(base_); }13 14#include <cassert>15#include <ranges>16#include <type_traits>17#include <utility>18 19#include "MoveOnly.h"20#include "types.h"21 22struct View : std::ranges::view_interface<View> {23 int i;24 int* begin() const;25 int* end() const;26};27 28struct MoveOnlyView : View {29 MoveOnly mo;30};31 32template <class T>33concept HasBase = requires(T&& t) { std::forward<T>(t).base(); };34 35struct Pred {36 constexpr bool operator()(int i) const { return i > 5; }37};38 39static_assert(HasBase<std::ranges::take_while_view<View, Pred> const&>);40static_assert(HasBase<std::ranges::take_while_view<View, Pred>&&>);41 42static_assert(!HasBase<std::ranges::take_while_view<MoveOnlyView, Pred> const&>);43static_assert(HasBase<std::ranges::take_while_view<MoveOnlyView, Pred>&&>);44 45constexpr bool test() {46 // const &47 {48 const std::ranges::take_while_view<View, Pred> twv{View{{}, 5}, {}};49 std::same_as<View> decltype(auto) v = twv.base();50 assert(v.i == 5);51 }52 53 // &54 {55 std::ranges::take_while_view<View, Pred> twv{View{{}, 5}, {}};56 std::same_as<View> decltype(auto) v = twv.base();57 assert(v.i == 5);58 }59 60 // &&61 {62 std::ranges::take_while_view<View, Pred> twv{View{{}, 5}, {}};63 std::same_as<View> decltype(auto) v = std::move(twv).base();64 assert(v.i == 5);65 }66 67 // const &&68 {69 const std::ranges::take_while_view<View, Pred> twv{View{{}, 5}, {}};70 std::same_as<View> decltype(auto) v = std::move(twv).base();71 assert(v.i == 5);72 }73 74 // move only75 {76 std::ranges::take_while_view<MoveOnlyView, Pred> twv{MoveOnlyView{{}, 5}, {}};77 std::same_as<MoveOnlyView> decltype(auto) v = std::move(twv).base();78 assert(v.mo.get() == 5);79 }80 return true;81}82 83int main(int, char**) {84 test();85 static_assert(test());86 return 0;87}88