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

            Bug ID: 109642
           Summary: False Positive -Wdangling-reference with
                    std::span-like classes
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlosgalvezp at gmail dot com
  Target Milestone: ---

Hi,

We are bumping our GCC installation from
6910cad55ffc330dc9767d2c8e0b66ccfa4134af to
cc035c5d8672f87dc8c2756d9f8367903aa72d93 (GCC 13.1 release), and are now
getting a lot of False Positives from code that looks like this:

#include <iterator>
#include <span>

template <typename T>
struct MySpan
{
 MySpan(T* data, std::size_t size) : 
    data_(data),
    size_(size)
 {}

 T& operator[](std::size_t idx) { return data_[idx]; }

private:
    T* data_;
    std::size_t size_;
};

template <typename T, std::size_t n>
MySpan<T const> make_my_span(T const(&x)[n])
{
    return MySpan(std::begin(x), n);
}

template <typename T, std::size_t n>
std::span<T const> make_span(T const(&x)[n])
{
    return std::span(std::begin(x), n);
}

int main()
{
    int x[10]{};
    int const& y{make_my_span(x)[0]};
    int const& y2{make_span(x)[0]};
}

Godbolt: https://godbolt.org/z/Pf6jsezoP

I.e. when using std::span, GCC is happy, but when using our own implementation
of span (since we can't enable C++20 yet in our project due to reasons), then
it complains about dangling reference. Clang trunk does not warn about this.

It warns both about non-const and const references.

Reply via email to