c++: avoid -Wdangling-reference for std::span-like classes [PR110358]
[official-gcc.git] / gcc / testsuite / g++.dg / warn / Wdangling-reference18.C
blobe088c177769a3bf95c6e0aee849e4dade9b7d2d7
1 // PR c++/110358
2 // { dg-do compile { target c++11 } }
3 // { dg-options "-Wdangling-reference" }
4 // Don't warn for std::span-like classes.
6 template <typename T>
7 struct Span {
8     T* data_;
9     int len_;
11     [[nodiscard]] constexpr auto operator[](int n) const noexcept -> T& { return data_[n]; }
12     [[nodiscard]] constexpr auto front() const noexcept -> T& { return data_[0]; }
13     [[nodiscard]] constexpr auto back() const noexcept -> T& { return data_[len_ - 1]; }
16 auto get() -> Span<int>;
18 auto f() -> int {
19     int const& a = get().front(); // { dg-bogus "dangling reference" }
20     int const& b = get().back();  // { dg-bogus "dangling reference" }
21     int const& c = get()[0];      // { dg-bogus "dangling reference" }
23     return a + b + c;