public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/95004] New: Static array of base classes member pointers
@ 2020-05-08  9:56 vince.rev at gmail dot com
  2020-05-08 12:22 ` [Bug c++/95004] " ppalka at gcc dot gnu.org
  2021-08-01  0:11 ` [Bug c++/95004] using the wrong pointer to member for base classes with two bases pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: vince.rev at gmail dot com @ 2020-05-08  9:56 UTC (permalink / raw)
  To: gcc-bugs

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/95004] Static array of base classes member pointers
  2020-05-08  9:56 [Bug c++/95004] New: Static array of base classes member pointers vince.rev at gmail dot com
@ 2020-05-08 12:22 ` ppalka at gcc dot gnu.org
  2021-08-01  0:11 ` [Bug c++/95004] using the wrong pointer to member for base classes with two bases pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: ppalka at gcc dot gnu.org @ 2020-05-08 12:22 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #1 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Reduced:

template <int> struct base {
    int value;
};

struct derived: base<0>, base<1> {
    using pointer_type = int derived::*;
    static constexpr pointer_type m = &base<1>::value;
};

static_assert(derived::m == &base<1>::value);

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/95004] using the wrong pointer to member for base classes with two bases
  2020-05-08  9:56 [Bug c++/95004] New: Static array of base classes member pointers vince.rev at gmail dot com
  2020-05-08 12:22 ` [Bug c++/95004] " ppalka at gcc dot gnu.org
@ 2021-08-01  0:11 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-01  0:11 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Static array of base        |using the wrong pointer to
                   |classes member pointers     |member for base classes
                   |                            |with two bases
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-08-01
      Known to fail|                            |6.4.0

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. To show it is not a problem with templates and just pointer to
members and not using aliases either.
Take:
struct base {
    int value1;
};
struct base0 {
    int value;
};
struct derived: base0, base {
    typedef int derived::* pointer_type ;
    static constexpr pointer_type m = &base0::value;
    static constexpr pointer_type m1 = &base::value1;
};
static_assert(derived::m == &base0::value,"");
static_assert(derived::m != &base::value1,"");
static_assert(derived::m1 != &base0::value,"");
static_assert(derived::m1 == &base::value1,"");

Basically the conversion from "int derived::*" to "int *base::" (or "int
*base0") is going wrong.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-08-01  0:11 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-08  9:56 [Bug c++/95004] New: Static array of base classes member pointers vince.rev at gmail dot com
2020-05-08 12:22 ` [Bug c++/95004] " ppalka at gcc dot gnu.org
2021-08-01  0:11 ` [Bug c++/95004] using the wrong pointer to member for base classes with two bases pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).