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

            Bug ID: 95004
           Summary: Static array of base classes member pointers
           Product: gcc
           Version: 10.1.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vince.rev at gmail dot com
  Target Milestone: ---

Created attachment 48480
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48480&action=edit
Example of bug with base member pointers

Consider the following code in std=c++17:
==================================================
#include <array>
#include <iostream>

template <std::size_t> struct base {
    std::size_t value;
};

struct derived: base<0>, base<1> {
    using pointer_type = std::size_t derived::*;
    static constexpr std::array<pointer_type, 2> members{{
        &derived::base<0>::value, 
        &derived::base<1>::value
    }};
    constexpr std::size_t& operator[](std::size_t i) noexcept {
        return this->*(members[i]);
    }
    constexpr const std::size_t& operator[](std::size_t i) const noexcept {
        return this->*(members[i]);
    }
};

int main(int, char**) {
    derived x{42, 84};
    std::cout << sizeof(base<0>) + sizeof(base<1>) << " " << sizeof(derived);
    std::cout << std::endl;
    std::cout << x[0] << " " << x[1]; // should display 42 84 but display 42 42
    std::cout << std::endl;
    return 0;
}
==================================================

It creates a templated structure "base" with a data member "value", and a
structure "derived" that inherits from several specializations of "base". The
code tries to access the "value" of one or the other "base" class depending on
an index provided at runtime. The provided code does not achieve this, and
always returns the value of the first "base".

The problem is also described here:
https://stackoverflow.com/questions/61675172/accessing-members-of-base-classes-in-the-derived-class-through-runtime-indexing

And clang produces the correct output:
https://godbolt.org/z/c72xLa

The bug was successfully reproduced on all version of gcc from 7.1 to 10.1

Reply via email to