public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision
@ 2004-01-27 10:19 smelkov at mph1 dot phys dot spbu dot ru
2004-01-27 10:20 ` [Bug c++/13881] " smelkov at mph1 dot phys dot spbu dot ru
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: smelkov at mph1 dot phys dot spbu dot ru @ 2004-01-27 10:19 UTC (permalink / raw)
To: gcc-bugs
It is possible to compare 'A C::*' with 'B C::*' (B inherits from A)
while cast from 'B C::*' to 'A C::*' is rejected.
I used to think that member pointers have same semantics when bound,
so if 'B*' can be casted to 'A*', someone might expect that 'B C::*' can be casted to 'A C::*'
(for example for plain non-virtual data-members).
see code below.
---
class A {};
class B : public A {};
class C {
public:
A a;
B b;
};
int main()
{
A C::* memptr_a = &C::a; // ok
B C::* memptr_b = &C::b; // ok
if (memptr_a == memptr_b) // ok
return 1;
if (&C::a == &C::b) // ok
return 1;
// but the following produce errors:
// error: invalid conversion from `B C::*' to `A C::*'
memptr_a = memptr_b;
// error: invalid conversion from `B C::*' to `A C::*'
A C::* memptr_a2 = &C::b;
return 0;
}
---
By the way: icc rejects comparision of 'A C::*' with 'B C::*'
so this bug is about
- 'invalid code accepted' (allows comparision), or
- 'valid code rejected' (rejects casting)
Can someone shed light on this subject?
--
Kirill.
--
Summary: pointer-to-data-member: rejects cast but allows
comparision
Product: gcc
Version: 3.3.2
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c++
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: smelkov at mph1 dot phys dot spbu dot ru
CC: gcc-bugs at gcc dot gnu dot org
GCC build triplet: i686-pc-linux-gnu
GCC host triplet: i686-pc-linux-gnu
GCC target triplet: i686-pc-linux-gnu
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13881
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/13881] pointer-to-data-member: rejects cast but allows comparision
2004-01-27 10:19 [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision smelkov at mph1 dot phys dot spbu dot ru
@ 2004-01-27 10:20 ` smelkov at mph1 dot phys dot spbu dot ru
2004-01-29 3:37 ` [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members giovannibajo at libero dot it
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: smelkov at mph1 dot phys dot spbu dot ru @ 2004-01-27 10:20 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From smelkov at mph1 dot phys dot spbu dot ru 2004-01-27 10:20 -------
forgot to include g++ version:
[kirr@tugrik test]$ g++ -v
Reading specs from /usr/bin/../lib/gcc-lib/i686-pc-linux-gnu/3.3.2/specs
Configured with: ../gcc-3.3.2/configure --prefix=/usr/local/gcc-3.3.2 --enable-threads=posix
--enable-languages=c,c++,f77 --enable-checking --disable-nls
Thread model: posix
gcc version 3.3.2
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13881
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members
2004-01-27 10:19 [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision smelkov at mph1 dot phys dot spbu dot ru
2004-01-27 10:20 ` [Bug c++/13881] " smelkov at mph1 dot phys dot spbu dot ru
@ 2004-01-29 3:37 ` giovannibajo at libero dot it
2004-01-29 12:26 ` smelkov at mph1 dot phys dot spbu dot ru
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: giovannibajo at libero dot it @ 2004-01-29 3:37 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From giovannibajo at libero dot it 2004-01-29 03:37 -------
You seem confused on the subject. "A C::*" and "B C::*" are totally unrelated
types. The default conversion could be applied if, e.g., you were to
compare "int B::*" and "int A::*", because A is an un-ambigous non-virtual
accessible base class of B, so the latter pointer can be converted to the
former type. Notice the direction that the conversion follows is the opposite
of that of normal pointers: in fact, every member of A is also a member of B,
so every "int A::*" is also a valid "int B::*". Instead, if you have a
generic "int B::*" you can't say for sure that it points to a member of A.
Look into [conv.mem]/2 in the ISO C++ Standard for further clarifications.
3.4/mainline follows the right behaviour, while the bug is still present on the
3.3 branch. Maybe we can pinpoint and backport the patch that fixed it.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |NEW
Ever Confirmed| |1
Keywords| |accepts-invalid
Known to fail| |3.3.3
Known to work| |3.4.0 3.5.0
Last reconfirmed|0000-00-00 00:00:00 |2004-01-29 03:37:26
date| |
Summary|pointer-to-data-member: |[3.3 Regression] Allow
|rejects cast but allows |comparison between unrelated
|comparision |pointer-to-data-members
Target Milestone|--- |3.3.3
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13881
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members
2004-01-27 10:19 [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision smelkov at mph1 dot phys dot spbu dot ru
2004-01-27 10:20 ` [Bug c++/13881] " smelkov at mph1 dot phys dot spbu dot ru
2004-01-29 3:37 ` [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members giovannibajo at libero dot it
@ 2004-01-29 12:26 ` smelkov at mph1 dot phys dot spbu dot ru
2004-01-29 16:38 ` giovannibajo at libero dot it
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: smelkov at mph1 dot phys dot spbu dot ru @ 2004-01-29 12:26 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From smelkov at mph1 dot phys dot spbu dot ru 2004-01-29 12:26 -------
(In reply to comment #2)
Giovanni,
> You seem confused on the subject. "A C::*" and "B C::*" are totally unrelated
> types. The default conversion could be applied if, e.g., you were to
> compare "int B::*" and "int A::*", because A is an un-ambigous non-virtual
> accessible base class of B, so the latter pointer can be converted to the
> former type.
I agree, that my writing was somewhat mislieading. I'll try to clarify this:
A and B is related types (A is base of B), hence B* can be casted to A*.
As to 'A C::*' and 'B C::*' they are pointers to A & B objects in specific scope (C class) --- i
think they are related too.
for example:
struct C {
A a;
B b;
};
C* pc;
A* pa;
B* pb; // B inherits from A
pa = pb; // ok
*pa=... // ok, dereference to B::A
// I used to think that binding memptr has the same semantics as dereferencing plain pointer of
// the same type, i.e. [see some_A_method() call below]
C c;
A C::* mem_pa;
B C::* mem_pb;
pa = &c.a; // ok
pb = &c.b; // ok
pa = &c.b; // !!! this is also correct!!!
mem_pa = &C::a; // ok
mem_pb = &C::b; // ok
mem_pa = &C::b; // ?wrong?
// but i want to do:
pa->some_A_method();
(c.*mem_pa).some_A_method();
// some_A_method() may be virtual thus calling B::some_A_method instead.
//
// with member pointers i can't do that.
-----
I digged the subject a bit, here is my understanding:
All i'm saying about is possible, but is not implemented for now.
The main reason for this is multiple inheritance (i wouldn't say about virtual and others),
hence for member pointer it is necessary to store some kind of offset (addres for plain
functions, vtable offset for virtual functions, class offset for data members, ets) and this delta
(for casting).
for pointers to member functions this is all done (sizeof(int (C::*)() on my machine is 8, the
structure contain __offset and __delta),
for pointers to data members it is not implemented (sizeof(int C::*) = 4)
> Notice the direction that the conversion follows is the opposite
> of that of normal pointers: in fact, every member of A is also a member of B,
> so every "int A::*" is also a valid "int B::*". Instead, if you have a
> generic "int B::*" you can't say for sure that it points to a member of A.
>
> Look into [conv.mem]/2 in the ISO C++ Standard for further clarifications.
>
>
> 3.4/mainline follows the right behaviour, while the bug is still present on the
> 3.3 branch. Maybe we can pinpoint and backport the patch that fixed it.
>
Thanks,
Kirill.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13881
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members
2004-01-27 10:19 [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision smelkov at mph1 dot phys dot spbu dot ru
` (2 preceding siblings ...)
2004-01-29 12:26 ` smelkov at mph1 dot phys dot spbu dot ru
@ 2004-01-29 16:38 ` giovannibajo at libero dot it
2004-02-02 9:19 ` smelkov at mph1 dot phys dot spbu dot ru
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: giovannibajo at libero dot it @ 2004-01-29 16:38 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From giovannibajo at libero dot it 2004-01-29 16:38 -------
Subject: Re: [3.3 Regression] Allow comparison between unrelated pointer-to-data-members
smelkov at mph1 dot phys dot spbu dot ru wrote:
> A and B is related types (A is base of B), hence B* can be casted to
> A*. As to 'A C::*' and 'B C::*' they are pointers to A & B objects in
> specific scope (C class) --- i think they are related too.
No, because they're basically offsets to A and B objects in C scope. Until
they're *bound* to a specific object, they don't allow any standard conversion
besides the one that I explained you. After you bind them (through
"operator ->*" or "operator .*"), they become regular A* or B*, and you can do
whatever you want.
> C c;
>
> A C::* mem_pa;
> B C::* mem_pb;
>
> pa = &c.a; // ok
> pb = &c.b; // ok
> pa = &c.b; // !!! this is also correct!!!
of course, because &c.b is a regular B*.
> mem_pa = &C::a; // ok
> mem_pb = &C::b; // ok
>
> mem_pa = &C::b; // ?wrong?
Yes, because the types don't match and there is no standard conversion that
applies.
> All i'm saying about is possible, but is not implemented for now.
The point is not what a compiler could do, but what it has to do. ISO C++
Standard forbids such conversions, and g++ won't allow them. Your report has
been useful since it made us discover an accepts-invalid on the 3.3 branch, so
that we can probably backport the fix and have it fixed for 3.3.4. g++ since
3.4.0 already implements the correct behaviour though.
I think you're using the wrong approach, hence the problems you're facing. If
you need some powerful and generic mechanism for function dispatch or object
visits, look into boost::function.
Giovanni Bajo
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13881
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members
2004-01-27 10:19 [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision smelkov at mph1 dot phys dot spbu dot ru
` (3 preceding siblings ...)
2004-01-29 16:38 ` giovannibajo at libero dot it
@ 2004-02-02 9:19 ` smelkov at mph1 dot phys dot spbu dot ru
2004-02-15 12:41 ` gdr at gcc dot gnu dot org
2004-04-18 18:52 ` gdr at gcc dot gnu dot org
6 siblings, 0 replies; 8+ messages in thread
From: smelkov at mph1 dot phys dot spbu dot ru @ 2004-02-02 9:19 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From smelkov at mph1 dot phys dot spbu dot ru 2004-02-02 09:19 -------
(In reply to comment #4)
Giovanni,
Of course you are right -- gcc shall follow ISO C++ standard.
I was just confused by the defference between member pointers to data and functions,
And of course gcc's bugzilla is wrong place for such a discussion.
Sorry for the noise,
Kirill.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13881
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members
2004-01-27 10:19 [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision smelkov at mph1 dot phys dot spbu dot ru
` (4 preceding siblings ...)
2004-02-02 9:19 ` smelkov at mph1 dot phys dot spbu dot ru
@ 2004-02-15 12:41 ` gdr at gcc dot gnu dot org
2004-04-18 18:52 ` gdr at gcc dot gnu dot org
6 siblings, 0 replies; 8+ messages in thread
From: gdr at gcc dot gnu dot org @ 2004-02-15 12:41 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From gdr at gcc dot gnu dot org 2004-02-15 12:41 -------
Adjust milestone
--
What |Removed |Added
----------------------------------------------------------------------------
Target Milestone|3.3.3 |3.3.4
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13881
^ permalink raw reply [flat|nested] 8+ messages in thread
* [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members
2004-01-27 10:19 [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision smelkov at mph1 dot phys dot spbu dot ru
` (5 preceding siblings ...)
2004-02-15 12:41 ` gdr at gcc dot gnu dot org
@ 2004-04-18 18:52 ` gdr at gcc dot gnu dot org
6 siblings, 0 replies; 8+ messages in thread
From: gdr at gcc dot gnu dot org @ 2004-04-18 18:52 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From gdr at gcc dot gnu dot org 2004-04-18 18:43 -------
Fixed in 3.4.0. Won't for 3.3.x
--
What |Removed |Added
----------------------------------------------------------------------------
Status|NEW |RESOLVED
Resolution| |FIXED
Target Milestone|3.3.4 |3.4.0
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13881
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2004-04-18 18:43 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-01-27 10:19 [Bug c++/13881] New: pointer-to-data-member: rejects cast but allows comparision smelkov at mph1 dot phys dot spbu dot ru
2004-01-27 10:20 ` [Bug c++/13881] " smelkov at mph1 dot phys dot spbu dot ru
2004-01-29 3:37 ` [Bug c++/13881] [3.3 Regression] Allow comparison between unrelated pointer-to-data-members giovannibajo at libero dot it
2004-01-29 12:26 ` smelkov at mph1 dot phys dot spbu dot ru
2004-01-29 16:38 ` giovannibajo at libero dot it
2004-02-02 9:19 ` smelkov at mph1 dot phys dot spbu dot ru
2004-02-15 12:41 ` gdr at gcc dot gnu dot org
2004-04-18 18:52 ` gdr at gcc dot gnu dot 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).