
On Fri, Sep 17, 2010 at 9:46 AM, <dherring@ll.mit.edu> wrote:
On Fri, 17 Sep 2010, Frank Mori Hess wrote:
On Friday 17 September 2010, dherring@ll.mit.edu wrote:
The "standard" workaround is to allocate your own memory and use manual type casting. Boost::aligned_storage can be used to maintain proper alignment, but it cannot be placed inside a union due to the default ctor, dtor, and noncopyable functions. Thus I am forced to use part of boost::detail.
Questions: - Is there a better way to do this?
What about using boost.variant instead?
It doesn't satisfy my need for everything to be in-place (e.g. for seamless use with shared memory, memory pools, etc.). [Note: we don't actually use std::string; it was just convenient for the example.]
How is it not in-place? If C++ supported unions containing anything, then boost::variant<int,std::string,myClass> would be identical in layout to: struct { uint which; union { int i; std::string s; myClass m; } } Which all use the same memory.