| 1 | // Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 | // |
| 3 | // This file is part of the GNU ISO C++ Library. This library is free |
| 4 | // software; you can redistribute it and/or modify it under the |
| 5 | // terms of the GNU General Public License as published by the |
| 6 | // Free Software Foundation; either version 3, or (at your option) |
| 7 | // any later version. |
| 8 | |
| 9 | // This library is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | |
| 14 | // You should have received a copy of the GNU General Public License along |
| 15 | // with this library; see the file COPYING3. If not see |
| 16 | // <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | // { dg-do compile { target c++20 } } |
| 19 | |
| 20 | #include <span> |
| 21 | |
| 22 | template<typename T, int N, typename U> |
| 23 | constexpr bool is_static_span(const U&) |
| 24 | { |
| 25 | return std::is_same_v<std::span<T, N>, U> && N != std::dynamic_extent; |
| 26 | } |
| 27 | |
| 28 | template<typename T, typename U> |
| 29 | constexpr bool is_dynamic_span(const U&) |
| 30 | { |
| 31 | return std::is_same_v<std::span<T>, U>; |
| 32 | } |
| 33 | |
| 34 | struct Range |
| 35 | { |
| 36 | float* begin() const; |
| 37 | float* end() const; |
| 38 | }; |
| 39 | |
| 40 | void |
| 41 | test01() |
| 42 | { |
| 43 | const char c[] = ""; |
| 44 | int i[2]{}; |
| 45 | std::array<long, 3> a; |
| 46 | Range r; |
| 47 | |
| 48 | std::span s1(c); |
| 49 | static_assert( is_static_span<const char, 1>(s1) ); |
| 50 | |
| 51 | std::span s2(i); |
| 52 | static_assert( is_static_span<int, 2>(s2) ); |
| 53 | |
| 54 | std::span s3(a); |
| 55 | static_assert( is_static_span<long, 3>(s3) ); |
| 56 | |
| 57 | std::span s4(const_cast<const std::array<long, 3>&>(a)); |
| 58 | static_assert( is_static_span<const long, 3>(s4) ); |
| 59 | |
| 60 | std::span s5(std::begin(i), std::end(i)); |
| 61 | static_assert( is_dynamic_span<int>(s5) ); |
| 62 | |
| 63 | std::span s6(std::cbegin(i), std::cend(i)); |
| 64 | static_assert( is_dynamic_span<const int>(s6) ); |
| 65 | |
| 66 | std::span s7(r); |
| 67 | static_assert( is_dynamic_span<float>(s7) ); |
| 68 | |
| 69 | std::span s8(s1); |
| 70 | static_assert( is_static_span<const char, 1>(s8) ); |
| 71 | |
| 72 | std::span s9(s2); |
| 73 | static_assert( is_static_span<int, 2>(s9) ); |
| 74 | |
| 75 | std::span s10(const_cast<const std::span<int, 2>&>(s2)); |
| 76 | static_assert( is_static_span<int, 2>(s10) ); |
| 77 | |
| 78 | std::span s11(s5); |
| 79 | static_assert( is_dynamic_span<int>(s11) ); |
| 80 | |
| 81 | std::span s12(const_cast<const std::span<int>&>(s5)); |
| 82 | static_assert( is_dynamic_span<int>(s12) ); |
| 83 | } |