Topic: C++0x POD


Author: Ares Lagae <ares.lagae@gmail.com>
Date: Thu, 16 Sep 2010 11:36:14 CST
Raw View
Hi all,

I have noticed that in C++0x, std::complex<T> is now equivalent with
T[2] and C99's complex, i.e., a reinterpret_cast<T*>() of
&std::complex<T> gives the expected result.

I was wondering, whether this follows from the relaxed C++0x rules of
POD's, or if this is still a specific / exceptional case?

Is the same behaviour guaranteed with my class template <typename T>
class vector_3d { T xyz_; }; ? And with arrays of std::complex<T> and
vector_3d<T>?

Thanks,

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use
mailto:std-c++@netlab.cs.rpi.edu<std-c%2B%2B@netlab.cs.rpi.edu>
]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sun, 19 Sep 2010 15:36:52 CST
Raw View
On 16 Sep., 19:36, Ares Lagae <ares.la...@gmail.com> wrote:
> Hi all,
>
> I have noticed that in C++0x, std::complex<T> is now equivalent with
> T[2] and C99's complex, i.e., a reinterpret_cast<T*>() of
> &std::complex<T> gives the expected result.
>
> I was wondering, whether this follows from the relaxed C++0x rules of
> POD's, or if this is still a specific / exceptional case?

Fortunately this is no "magic rule", but this conclusion can be drawn
if you assume that std::complex<T> is a standard-layout class type
(see clause 9), which is a very reasonable assumption anyway. Now
we have 9.2/19:

"A pointer to a standard-layout struct object, suitably converted
using
a reinterpret_cast, points to its initial member (or if that member is
a
bit-field, then to the unit in which it resides) and vice versa.
[ Note:
There might therefore be unnamed padding within a standard-layout
struct object, but not at its beginning, as necessary to achieve
appropriate alignment. =97end note ]"

This is essentially the guarantee we need: Obviously the first data
member has to be an array of two T.

> Is the same behaviour guaranteed with my class template <typename T>
> class vector_3d { T xyz_; }; ? And with arrays of std::complex<T> and
> vector_3d<T>?

It depends on the T. If T is standard-layout type (This is guaranteed
for std::complex which only directly supports float, double, or long
double), you can rely on the fact that you can do a reinterpret_cast
to a pointer of vector_3d<T> to obtain a valid pointer to T.

I see nothing that would give arrays a special rule: Every element
of an array of T should be equivalent to a single T object and
thus the rule should apply for each element of arrays.

HTH & Greetings from Bremen,

Daniel Kr=FCgler

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: =?ISO-8859-1?Q?Daniel_Kr=FCgler?= <daniel.kruegler@googlemail.com>
Date: Sun, 19 Sep 2010 18:14:56 CST
Raw View
[Second attempt to get my reply through]

On 16 Sep., 19:36, Ares Lagae <ares.la...@gmail.com> wrote:
> I have noticed that in C++0x, std::complex<T> is now equivalent with
> T[2] and C99's complex, i.e., a reinterpret_cast<T*>() of
> &std::complex<T> gives the expected result.
>
> I was wondering, whether this follows from the relaxed C++0x rules of
> POD's, or if this is still a specific / exceptional case?

This is no magic rule, but just an implied usage of existing rules
in regard to standard-layout types. The relevant paragraph is (all
quotes from N3126):

9.2/19:

"A pointer to a standard-layout struct object, suitably converted
using
a reinterpret_cast, points to its initial member (or if that member is
a
bit-field, then to the unit in which it resides) and vice versa.
[ Note:
There might therefore be unnamed padding within a standard-layout
struct object, but not at its beginning, as necessary to achieve
appropriate alignment.    end note ]".

Thus, above quoted requirements can be realized by defining a class
type that satisfies the standard-layout type conditions (see 9 [class]/
6)
and using an array type as a first (non-static) data member.

> Is the same behaviour guaranteed with my class template <typename T>
> class vector_3d { T xyz_; }; ? And with arrays of std::complex<T> and
> vector_3d<T>?

Yes, this is guaranteed if and only if T itself is a standard-layout
type,
because the definition of a standard-layout type is recursive. It
also
applies to arrays, because arrays of standard-layout types are
similarly
standard-layout-types (see [basic.types]/9) and a pointer to an array
element of type T is equivalent to a pointer to T.

HTH & Greetings from Bremen,

Daniel Kr   gler

--
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@netlab.cs.rpi.edu]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]