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
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).