| 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. |
| 5 |
|
| 6 |
template <typename T> |
| 7 |
struct Span { |
| 8 |
T* data_; |
| 9 |
int len_; |
| 10 |
~Span (); |
| 11 |
|
| 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]; } |
| 15 |
}; |
| 16 |
|
| 17 |
auto get() -> Span<int>; |
| 18 |
|
| 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" } |
| 23 |
|
| 24 |
return a + b + c; |
| 25 |
} |