https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110358

            Bug ID: 110358
           Summary: requesting nicer suppression for Wdangling-reference
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

As everyone is already aware, Wdangling-reference gives false positives for
reference-semantic classes. The compiler has special cases for the ones it
knows about, but not for mine. So:

template <typename T>
struct Span {
    T* data_;
    int len_;

    [[nodiscard]] constexpr auto operator[](int n) const noexcept -> T& {
return data_[n]; }
    [[nodiscard]] constexpr auto front() const noexcept -> T& { return
data_[0]; }
    [[nodiscard]] constexpr auto back() const noexcept -> T& { return
data_[len_ - 1]; }
};

auto get() -> Span<int>;

auto f() -> int {
    int const& a = get().front(); // warning
    int const& b = get().back();  // warning
    int const& c = get()[0];      // warning

    return a + b + c;
}

The suppression for this is to #pragma around all my functions, which is a bit
of a tedious suppression, since this is two prefix lines and one postfix line
(but for libraries it's actually 4 prefix lines because we can't ignore
Wdangling-reference until gcc 13, so need to #ifdef that out).

It'd be nice if we could:

* add an attribute to a class to conditionally suppress the warning
* add an attribute to a function to conditionally suppress the warning
* both

Basically in this case to let me do something like:

template <typename T>
struct [[gnu::marek_is_awesome_but_this_warning_has_too_many_false_positives]]
Span {
   // ...
};

And I say conditional so that I can do this (because Optional<int&> wants to
suppress the warning, but Optional<int> definitely does not!)

template <typename T>
struct
[[gnu::marek_is_awesome_but_this_warning_has_too_many_false_positives(std::is_reference_v<T>)]]
Optional {
   // ...
};

Obviously I am not the best at naming.

Reply via email to