Debian Codegraph @

gcc/testsuite/g++.dg/warn/Wdangling-reference20.C

raw
1 // PR c++/109640
2 // { dg-do compile { target c++20 } }
3 // { dg-options "-Wdangling-reference" }
4 // Don't warn for std::span-like classes.
5
6 #include <iterator>
7 #include <span>
8
9 template <typename T>
10 struct MySpan
11 {
12 MySpan(T* data, std::size_t size) :
13 data_(data),
14 size_(size)
15 {}
16
17 T& operator[](std::size_t idx) { return data_[idx]; }
18
19 private:
20 T* data_;
21 std::size_t size_;
22 };
23
24 template <typename T, std::size_t n>
25 MySpan<T const> make_my_span(T const(&x)[n])
26 {
27 return MySpan(std::begin(x), n);
28 }
29
30 template <typename T, std::size_t n>
31 std::span<T const> make_span(T const(&x)[n])
32 {
33 return std::span(std::begin(x), n);
34 }
35
36 int main()
37 {
38 int x[10]{};
39 [[maybe_unused]] int const& y1{make_my_span(x)[0]};
40 [[maybe_unused]] int const& y2{make_span(x)[0]};
41 using T = int[10];
42 [[maybe_unused]] int const& y3{make_my_span(T{})[0]}; // { dg-warning "dangling reference" }
43 [[maybe_unused]] int const& y4{make_span(T{})[0]}; // { dg-warning "dangling reference" }
44 }