Debian Codegraph @

gcc/testsuite/g++.dg/ext/attr-no-dangling3.C

raw
1 // { dg-do compile { target c++11 } }
2 // { dg-options "-Wdangling-reference" }
3
4 template <typename T>
5 struct [[gnu::no_dangling]] Span {
6 T* data_;
7 int len_;
8 // So that our heuristic doesn't suppress the warning anyway.
9 ~Span();
10
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]; }
14 };
15
16 auto get() -> Span<int>;
17
18 auto f() -> int {
19 int const& a = get().front(); // { dg-bogus "dangling" }
20 int const& b = get().back(); // { dg-bogus "dangling" }
21 int const& c = get()[0]; // { dg-bogus "dangling" }
22
23 return a + b + c;
24 }