c++: avoid -Wdangling-reference for std::span-like classes [PR110358]
[official-gcc.git] / gcc / testsuite / g++.dg / warn / Wdangling-reference19.C
blob053467d822f35de519126b1fd9e6963e56854928
1 // PR c++/110358
2 // { dg-do compile { target c++11 } }
3 // { dg-options "-Wdangling-reference" }
4 // Like Wdangling-reference18.C but not actually a span-like class.
6 template <typename T>
7 struct Span {
8     T* data_;
9     int len_;
10     ~Span ();
12     [[nodiscard]] constexpr auto operator[](int n) const noexcept -> T& { return data_[n]; }
13     [[nodiscard]] constexpr auto front() const noexcept -> T& { return data_[0]; }
14     [[nodiscard]] constexpr auto back() const noexcept -> T& { return data_[len_ - 1]; }
17 auto get() -> Span<int>;
19 auto f() -> int {
20     int const& a = get().front(); // { dg-warning "dangling reference" }
21     int const& b = get().back();  // { dg-warning "dangling reference" }
22     int const& c = get()[0];      // { dg-warning "dangling reference" }
24     return a + b + c;