summaryrefslogtreecommitdiff
path: root/libstdc++-v3/testsuite/std/ranges/access/cbegin.cc
blob: 6e994210c6b89c6b4a682c695db7c399ee5f9b0b (plain)
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
// Copyright (C) 2019-2020 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING3.  If not see
// <http://www.gnu.org/licenses/>.

// { dg-options "-std=gnu++2a" }
// { dg-do run { target c++2a } }

#include <ranges>
#include <testsuite_hooks.h>
using std::same_as;

void
test01()
{
  int a[2] = {};

  static_assert(same_as<decltype(std::ranges::cbegin(a)), const int*>);
  static_assert(noexcept(std::ranges::cbegin(a)));
  VERIFY( std::ranges::cbegin(a) == (a + 0) );

  constexpr long b[2] = {};
  static_assert( std::ranges::cbegin(b) == (b + 0) );
}

struct R
{
  int a[4] = { 0, 1, 2, 3 };

  friend int* begin(R& r) { return r.a + 0; }
  friend int* begin(R&&); // this function is not defined
  friend const int* begin(const R& r) noexcept { return r.a + 2; }
  friend const int* begin(const R&&); // this function is not defined
};

struct RV // view on an R
{
  R& r;

  friend int* begin(RV&); // this function is not defined
  friend const int* begin(const RV& rv) noexcept { return begin(std::as_const(rv.r)); }
};

// Allow ranges::begin to work with RV&&
template<> constexpr bool std::ranges::enable_borrowed_range<RV> = true;

void
test03()
{
  R r;
  const R& c = r;
  VERIFY(std::ranges::cbegin(r) == std::ranges::begin(c));
  VERIFY(std::ranges::cbegin(c) == std::ranges::begin(c));

  RV v{r};
  VERIFY(std::ranges::cbegin(std::move(v)) == std::ranges::begin(c));
  const RV cv{r};
  VERIFY(std::ranges::cbegin(std::move(cv)) == std::ranges::begin(c));
}

struct RR
{
  short s = 0;
  long l = 0;
  int a[4] = { 0, 1, 2, 3 };

  short* begin() noexcept { return &s; }
  const long* begin() const { return &l; }

  friend int* begin(RR& r) { return r.a + 0; }
  friend int* begin(RR&& r) { return r.a + 1; }
  friend const int* begin(const RR& r) { return r.a + 2; }
  friend const int* begin(const RR&& r) noexcept { return r.a + 3; }
};

// N.B. this is a lie, cbegin on an RR rvalue will return a dangling pointer.
template<> constexpr bool std::ranges::enable_borrowed_range<RR> = true;

void
test04()
{
  RR r;
  const RR& c = r;
  VERIFY(std::ranges::cbegin(r) == std::ranges::begin(c));
  VERIFY(std::ranges::cbegin(std::move(r)) == std::ranges::begin(c));
  VERIFY(std::ranges::cbegin(c) == std::ranges::begin(c));
  VERIFY(std::ranges::cbegin(std::move(c)) == std::ranges::begin(c));
}

int
main()
{
  test01();
  test03();
  test04();
}