| 1 |
// { dg-do compile { target c++11 } } |
| 2 |
|
| 3 |
// Copyright (C) 2010-2025 Free Software Foundation, Inc. |
| 4 |
// |
| 5 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 6 |
// software; you can redistribute it and/or modify it under the |
| 7 |
// terms of the GNU General Public License as published by the |
| 8 |
// Free Software Foundation; either version 3, or (at your option) |
| 9 |
// any later version. |
| 10 |
|
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
// GNU General Public License for more details. |
| 15 |
|
| 16 |
// You should have received a copy of the GNU General Public License along |
| 17 |
// with this library; see the file COPYING3. If not see |
| 18 |
// <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
// C++ 2011 24.6.5, range access [iterator.range] |
| 21 |
|
| 22 |
#include <iterator> |
| 23 |
|
| 24 |
void |
| 25 |
test01() |
| 26 |
{ |
| 27 |
int arr[3] = {1, 2, 3}; |
| 28 |
(void) std::begin(arr); |
| 29 |
(void) std::end(arr); |
| 30 |
|
| 31 |
static_assert( noexcept(std::begin(arr)), "LWG 2280" ); |
| 32 |
static_assert( noexcept(std::end(arr)), "LWG 2280" ); |
| 33 |
} |
| 34 |
|
| 35 |
void |
| 36 |
test02() |
| 37 |
{ |
| 38 |
extern void require_int(int*); |
| 39 |
extern void require_long(long*); |
| 40 |
|
| 41 |
struct B |
| 42 |
{ |
| 43 |
int* begin() { return nullptr; } |
| 44 |
long* begin() const { return nullptr; } |
| 45 |
}; |
| 46 |
|
| 47 |
B b; |
| 48 |
require_int( std::begin(b) ); |
| 49 |
require_long( std::begin(const_cast<const B&>(b)) ); |
| 50 |
|
| 51 |
struct E |
| 52 |
{ |
| 53 |
int* end() { return nullptr; } |
| 54 |
long* end() const { return nullptr; } |
| 55 |
}; |
| 56 |
|
| 57 |
E e; |
| 58 |
require_int( std::end(e) ); |
| 59 |
require_long( std::end(const_cast<const E&>(e)) ); |
| 60 |
|
| 61 |
static_assert( ! noexcept(std::begin(b)), "throws" ); |
| 62 |
static_assert( ! noexcept(std::begin(const_cast<const B&>(b))), "throws" ); |
| 63 |
static_assert( ! noexcept(std::end(e)), "throws" ); |
| 64 |
static_assert( ! noexcept(std::end(const_cast<const E&>(e))), "throws" ); |
| 65 |
|
| 66 |
struct S |
| 67 |
{ |
| 68 |
int* begin() noexcept { return nullptr; } |
| 69 |
int* begin() const noexcept { return nullptr; } |
| 70 |
int* end() noexcept { return nullptr; } |
| 71 |
int* end() const noexcept { return nullptr; } |
| 72 |
}; |
| 73 |
S s; |
| 74 |
static_assert( noexcept(std::begin(s)), "nothrow" ); |
| 75 |
static_assert( noexcept(std::begin(const_cast<const S&>(s))), "nothrow" ); |
| 76 |
static_assert( noexcept(std::end(s)), "nothrow" ); |
| 77 |
static_assert( noexcept(std::end(const_cast<const S&>(s))), "nothrow" ); |
| 78 |
} |