473,396 Members | 1,738 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,396 software developers and data experts.

Casting pointer to members

Compiling the following code (gcc 3.3.6), I get an invalid conversion
error.
Is there a valid reason why this conversion can't be performed?

---

class A {};
class B : public A {};

class T {
public:
B b;
};

int main() {
A T::*a = &T::b;
}

---

Paolo Capriotti

Oct 3 '05 #1
4 1232
* Paolo Capriotti:
Compiling the following code (gcc 3.3.6), I get an invalid conversion
error.
Is there a valid reason why this conversion can't be performed?

class A {};
class B : public A {};

class T {
public:
B b;
};

int main() {
A T::*a = &T::b;
}


For the purposes of this group, the explanation is that §3.9.2/3 tells us that
statements regarding pointers do not apply to pointers to members (except for
pointers to static members), so that the general B* -> A* conversion doesn't
apply, and then §4.11 does not list the conversion above for member pointers.

As to why, I think that's a defect in the standard.

I cannot think of any reason why the standard should forbid this conversion,
but then I don't understand e.g. how the dynamic type of an object that a
pointer to member is applied to, can not contain that member, as mentioned in
§5.5/4 (except for perhaps pure virtual functions, but that's covered
elsewhere and I think if it was that then the standard would just say so).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 3 '05 #2
Alf P. Steinbach wrote:
* Paolo Capriotti:
Compiling the following code (gcc 3.3.6), I get an invalid conversion
error.
Is there a valid reason why this conversion can't be performed?

class A {};
class B : public A {};

class T {
public:
B b;
};

int main() {
A T::*a = &T::b;
}


For the purposes of this group, the explanation is that §3.9.2/3 tells us that
statements regarding pointers do not apply to pointers to members (exceptfor
pointers to static members), so that the general B* -> A* conversion doesn't
apply, and then §4.11 does not list the conversion above for member pointers.

As to why, I think that's a defect in the standard.

I cannot think of any reason why the standard should forbid this conversion,
but then I don't understand e.g. how the dynamic type of an object that a
pointer to member is applied to, can not contain that member, as mentioned in
§5.5/4 (except for perhaps pure virtual functions, but that's covered
elsewhere and I think if it was that then the standard would just say so).


An object of class B may have stricter alignment requirements than an
object of class A. As a consequence, it is not safe to assume that an A
T::* member pointer can be safely converted to a B T::* member pointer.
They may in point to different locations in A and B respectively.

But as long as B is a subclass of A - and its alignment requirements
are no stricter than A's - then it is safe to use reinterpret_cast to
convert from A T::* to an B T::* (see 5.2/9).

Greg

Oct 3 '05 #3
* Greg:

An object of class B may have stricter alignment requirements than an
object of class A. As a consequence, it is not safe to assume that an A
T::* member pointer can be safely converted to a B T::* member pointer.
They may in point to different locations in A and B respectively.

But as long as B is a subclass of A - and its alignment requirements
are no stricter than A's - then it is safe to use reinterpret_cast to
convert from A T::* to an B T::* (see 5.2/9).


I don't understand what you're saying, in particular the conversion you're
discussing seems to be opposite of the original example, and using the result
of a one-way reinterpret_cast is simply undefined (do you perhaps mean
in-practice safe?), but I see that alignment requirements are mentioned for
round-trip (there and back again) reinterpret_cast discussed in §5.2.10/9.

Could you give an example or elaboration?

Anyway, thanks for the reference to §5.2/9, I assume you meant §5.2.9/9?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 3 '05 #4
class A {};
class B : public A {};

class T {
public:
B b;
};

int main() {
T t;
A*a = &t.b;
}

Oct 3 '05 #5

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

Similar topics

4
by: Jacob Jensen | last post by:
This question has probably been asked a million time, but here it comes again. I want to learn the difference between the three type cast operators: static_cast, reinterpret_cast, dynamic_cast. A...
5
by: Ingo Nolden | last post by:
Hi there, I am writing a smart pointer that is similar to the boost intrusive ptr. I am trying to make it behave like a c++ pointer including the implicit and explicit casting behaviour. Below...
3
by: Rob Jackson | last post by:
HiI've got a struct, known by file A.c, which contains a pointer to struct B. Struct B is unknown by file A.c (it is declared in C.h), and contains a typedef enum, which is declared in a file B.h,...
3
by: joe bruin | last post by:
hello all. i am trying to get rid of some warnings and do "the right thing". although in this particular case, i am not sure what the right thing is. the code: typedef struct {
20
by: j0mbolar | last post by:
I was reading page 720 of unix network programming, volume one, second edition. In this udp_write function he does the following: void udp_write(char *buf, <everything else omitted) struct...
17
by: goldfita | last post by:
I saw some code that appeared to do something similar to this struct foo { char offset; int d; }; struct foo { int a; int b;
8
by: wkaras | last post by:
In my compiler, the following code generates an error: union U { int i; double d; }; U u; int *ip = &u.i; U *up = static_cast<U *>(ip); // error I have to change the cast to...
4
by: Schkhan | last post by:
Hi, I am sturggling with a peace of code mainly its about casting a pointer to one type of structure to a pointer to another type of structure. the confusion begins with the following...
9
by: Taras_96 | last post by:
Hi everyone, I was experimenting with static_cast and reinterpret cast #include <iostream> struct A1 { int a; }; struct A2 { double d; }; struct B : public A1, A2
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.