473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

pointer-to-member comparison

Consider this compilation unit:

struct Base {};
struct A : public Base { int x; };
struct B : public Base { int y; };
int Base::*const px = static_cast<int Base::*>(&A::x);
int Base::*const py = static_cast<int Base::*>(&B::y);

bool f() {
return px == py;
}

First, is the program ill-formed? Or is the behavior undefined?
5.10p2 says px == py "if and only if they would refer to the same
member of the same most derived object (1.8) or the same subobject if
they were dereferenced with a hypothetical object of the associated
class type." I assume the "associated class type" here is Base. But
5.2.9p9 and 5.5p4 make it clear that dereferencing an object with
dynamic type Base using px or py gives undefined behavior.

If the above is ill-formed or undefined, does it follow that the
expression (px == px) has the same problem?

Not too surprisingly, my compiler has the above f() return true. If
this happens, it seems reasonable to assume that the following also
always returns true, although the a.*py is technically at best
undefined:
bool g( A& a ) {
return &(a.*py) == &a.x;
}

Realistically, it seems like I can count on the "logic" that f()
implies g(), even though the Standard doesn't technically guarantee
this. But if any of this is ill-formed, I want to avoid it.
Nov 5 '08 #1
2 1296
as*******@gmail.com wrote:
Consider this compilation unit:

struct Base {};
struct A : public Base { int x; };
struct B : public Base { int y; };
int Base::*const px = static_cast<int Base::*>(&A::x);
int Base::*const py = static_cast<int Base::*>(&B::y);

bool f() {
return px == py;
}

First, is the program ill-formed?
No. Every statement here is perfectly well-formed.
Or is the behavior undefined?
I'd expect so. Although the language specification doesn't seem to
address this situation.
5.10p2 says px == py "if and only if they would refer to the same
member of the same most derived object (1.8) or the same subobject if
they were dereferenced with a hypothetical object of the associated
class type." I assume the "associated class type" here is Base.
I'd also assume the same thing.
But
5.2.9p9 and 5.5p4 make it clear that dereferencing an object with
dynamic type Base using px or py gives undefined behavior.
Exactly.
If the above is ill-formed or undefined, does it follow that the
expression (px == px) has the same problem?
Hm... Good question. Formally speaking, it appears to be so.

--
Best regards,
Andrey Tarasevich
Nov 5 '08 #2
On Nov 5, 8:27 pm, aschep...@gmail.com wrote:
Consider this compilation unit:
struct Base {};
struct A : public Base { int x; };
struct B : public Base { int y; };
int Base::*const px = static_cast<int Base::*>(&A::x);
int Base::*const py = static_cast<int Base::*>(&B::y);
bool f() {
return px == py;
}
First, is the program ill-formed? Or is the behavior undefined?
5.10p2 says px == py "if and only if they would refer to the
same member of the same most derived object (1.8) or the same
subobject if they were dereferenced with a hypothetical object
of the associated class type." I assume the "associated class
type" here is Base. But 5.2.9p9 and 5.5p4 make it clear that
dereferencing an object with dynamic type Base using px or py
gives undefined behavior.
If the above is ill-formed or undefined, does it follow that
the expression (px == px) has the same problem?
I don't think that the standard is really clear about this. All
of the statements until the == are well formed and fully
defined. The return statement with the == is also well formed,
but I'm not sure what the results are supposed to be. It's not
clear here what the "associated class type" is; if you read this
to mean that your px == py is semantically equivalent to
&someBObject.x == &someBObject.y, then you have a real problem,
because of course, that expression won't even compile; if you
replace the "someBObject."s with "ptrToSomeBObject->", then your
code would have undefined behavior. Which, I suspect, is
probably the intent (if one can speak of "intent" about a case
which was apparently not considered); any time the standard
doesn't define a specific behavior, the behavior is undefined.
(In this case, it would seem reasonable to have the results
unspecified, rather than the behavior undefined, but in
practice, I doubt that changes anything for real programs.)
Not too surprisingly, my compiler has the above f() return
true. If this happens, it seems reasonable to assume that the
following also always returns true, although the a.*py is
technically at best undefined:
bool g( A& a ) {
return &(a.*py) == &a.x;
}
If the comparison is undefined behavior, then you can't count on
anything. Until proof of the contrary, I think that this is
what you have to assume.
Realistically, it seems like I can count on the "logic" that
f() implies g(), even though the Standard doesn't technically
guarantee this.
I don't think so. It's quite possible for a compiler to include
type information in a pointer to member, which would invalidate
your assumptions.
But if any of this is ill-formed, I want to avoid it.
Not ill-formed, but undefined behavior is probably something you
should avoid as well.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 6 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
20
by: joe | last post by:
Hi all! I just have quick, possibly stupid question.... is it possible to do the following: int func(){ int *pointer; foo(pointer); } int foo(int *pointer){
20
by: Bill Potter | last post by:
I am a learning programmer in C and i want to know why some one would use pointers instead of going direct!
13
by: william | last post by:
code segment: long int * size; char entry; ............. size=&entry; ************************************* Gcc reported 'assignment of incompatible pointer type'.
34
by: sumedh..... | last post by:
double * X size of X->?? size of X->?? double (*X) size of X->?? size of X->??
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.