Topic: address of contained member
Author: "joel de guzman" <joel@interxys.com>
Date: Thu, 12 Apr 2001 21:29:32 GMT Raw View
Hi,
Given:
template <typename T>
struct Y {
T x;
};
Is it safe to assume that the address of any Y object is the
same as the address of its member x?
Thanks,
Joel de Guzman
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Fri, 13 Apr 2001 10:44:42 GMT Raw View
joel de guzman wrote:
>
> Hi,
>
> Given:
>
> template <typename T>
> struct Y {
> T x;
> };
>
> Is it safe to assume that the address of any Y object is the
> same as the address of its member x?
No. According to 9.2p17, "A pointer to a POD-struct object, suitably
converted using a reinterpret_cast, points to its initial member (of if
that member is a bit-field, then to the unit in which it resides) and
vice versa." Therefore, the guarantee doesn't hold if T is a type that
renders Y<T> a non-POD struct. For instance, if T was a member pointer,
or a reference type, a non-POD struct or non-POD union type, or a
function type, then there's no guarantee. In particular, the address
can't possibly be the same as the address of x, if T is a function type,
since in that case x would be a member function.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Fri, 13 Apr 2001 10:46:09 GMT Raw View
In article <9b36q3$7lvre$1@ID-58159.news.dfncis.de>, joel de guzman says...
>
>Hi,
>
>Given:
>
>template <typename T>
>struct Y {
> T x;
>};
>
>
>Is it safe to assume that the address of any Y object is the
>same as the address of its member x?
>
>Thanks,
>Joel de Guzman
Each class Y<T> is a POD, and the first member of a POD
has the same address as the structure itself. (9.2/17)
So the answer is yes.
Adding copy assignment ctors and dtors, or non-POD types to the class
may break this relation.
Regards,
--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Steve Clamage <stephen.clamage@sun.com>
Date: Fri, 13 Apr 2001 16:22:40 GMT Raw View
joel de guzman wrote:
>
> Given:
>
> template <typename T>
> struct Y {
> T x;
> };
>
> Is it safe to assume that the address of any Y object is the
> same as the address of its member x?
>
Only if Y<T> is a POD-struct, which in this case requires that T be a POD type.
The exact definition of a POD (Plain Old Data) type is a bit
complicated. Any data type that can be declared in C is a POD type.
If Y or T has C++ features, you need to refer to the C++ standard,
section 9 paragraph 4, which in turn refers to you section 8.5.1,
for full details.
--
Steve Clamage, stephen.clamage@sun.com
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Sun, 15 Apr 2001 01:06:26 GMT Raw View
Michiel Salters wrote:
>
> In article <9b36q3$7lvre$1@ID-58159.news.dfncis.de>, joel de guzman says...
> >
> >Hi,
> >
> >Given:
> >
> >template <typename T>
> >struct Y {
> > T x;
> >};
> >
> >
> >Is it safe to assume that the address of any Y object is the
> >same as the address of its member x?
> >
> >Thanks,
> >Joel de Guzman
>
> Each class Y<T> is a POD, and the first member of a POD
> has the same address as the structure itself. (9.2/17)
struct Z{
int i;
Z(int in) i(in) {};
};
union U{
int j;
Z z;
};
None of the following are POD classes: Y<int&>, Y<Z>, Y<U>, Y<int Z::*>.
And how about:
typedef void void_func();
Y<void_func>::x is a member function, rather than a data member; as
such, it can't have the same address as the structure itself.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
Author: Michiel Salters<Michiel.Salters@cmg.nl>
Date: Tue, 17 Apr 2001 13:14:01 GMT Raw View
In article <3AD6F375.43220D23@wizard.net>, James Kuyper Jr. says...
>
>Michiel Salters wrote:
>>
>> In article <9b36q3$7lvre$1@ID-58159.news.dfncis.de>, joel de guzman says...
>> >
>> >Hi,
>> >
>> >Given:
>> >
>> >template <typename T>
>> >struct Y {
>> > T x;
>> >};
>> >
>> >Is it safe to assume that the address of any Y object is the
>> >same as the address of its member x?
>> >
>> >Thanks,
>> >Joel de Guzman
>>
>> Each class Y<T> is a POD, and the first member of a POD
>> has the same address as the structure itself. (9.2/17)
>None of the following are POD classes: Y<int&>, Y<Z>, Y<U>, Y<int Z::*>.
You're right, I should have added "if and only if T is a POD itself."
>And how about:
>
> typedef void void_func();
>
>Y<void_func>::x is a member function, rather than a data member; as
>such, it can't have the same address as the structure itself.
I don't think so. Y<void_func>::x is a pointer to a non-member function.
And because it is a pointer, it can have offset of 0 in the structure.
I can't think of a way to create a member function other than writing
class { // or struct
Return_Type MethodName (Argument_opt) // { optional implementation }
}
--
Michiel Salters
Consultant Technical Software Engineering
CMG Trade, Transport & Industry
Michiel.Salters@cmg.nl
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]