aligned_storage / alignment_of / type_with_alignment
If for some crazy reason I want/need a uninitialized type T, how do I use
type_with_alignment, etc to make it work?
eg something vaguely like this:
template <typename T>
struct uninitted
{
type_with_alignment
Gottlob Frege ha escrito:
If for some crazy reason I want/need a uninitialized type T, how do I use type_with_alignment, etc to make it work?
eg something vaguely like this:
template <typename T> struct uninitted { type_with_alignment
::type myT; void init_later(some_params) { new (&myT) T(some_params); } };
Does the in-place new work? Is the unitted<T> 'just like' T in terms of memory, usage within a struct, etc?
This is *almost* correct. There's a typo in 1.33 Boost.TypeTraits docs
(corrected
in 1.33.1) by which the explanations on type_with_alignment and
aligned_storage
are swapped. So, what you want is
template <typename T>
struct uninitted
{
aligned_storage
struct Foo1 { Bar bar; SomeT t; Foo foo; };
struct Foo2 { Bar bar; uninitted<SomeT> t; Foo foo; };
sizeof(Foo1) == sizeof(Foo2) offsetof(Foo1, t) == offsetof(Foo2, t) offsetof(Foo1, foo) == offsetof(Foo2, foo)
all true?
I think so (provided Bar and Foo are POD types, of course.) Not that the standard says so explicitly, but seems like you can deduce it from guarantees on PODs given in 9.2 [class.mem] plus TR1 reference on aligned_storage.
Thanks Tony
Best, Joaquín M López Muñoz Telefónica, Investigación y Desarrollo
participants (2)
-
Gottlob Frege
-
Joaquín Mª López Muñoz