Array subscript -N is outside array bounds warning in GCC11

Consider the code below with UB:

template <class Derived>
class A
{
public:

    Derived * get() { return static_cast<Derived *>(this);}

private:

    int m_a;
};

class B
{
public:

    double m_b;
};

class X : public B, public A<X>
{
public:

    int m_x;
};

int main()
{
    A<X> a;
    std::cout << a.get();
    return 0;
}

GCC11 compiles it with the warning:

array subscript -2 is outside array bounds of ‘A<X> [1]’ [-Warray-bounds]
Derived * get() { return static_cast<Derived *>(this);}

Leave a Reply

Your email address will not be published.