|
Boost Users : |
From: Joaquín Mª López Muñoz (joaquin_at_[hidden])
Date: 2005-10-07 01:32:34
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<sizeof(T), alignment_of<T>::value_type>::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<sizeof(T), alignment_of<T>::value>::type myT;
void init_later(some_params)
{
new (&myT) T(some_params);
}
};
Apart from this, your construct is OK and works as you expect. Actually,
this is
what aligned_storage is meant to be used for.
>
>
> 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
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net