id,summary,reporter,owner,description,type,status,milestone,component,version,severity,resolution,keywords,cc 5172,Boost Optional enhancement,nn-mail@…,Fernando Cacciola,"Boost.Optional is a very handy class.[[BR]] It can be extended to allow more flexibility. [[BR]] Proposal:[[BR]] Add additional argument with size to optional.[[BR]] Then the storage is created with the given size without checking sizeof(T).[[BR]] And only in the construct function the size is being checked.[[BR]] If the size is less than sizeof(T) , give error.[[BR]] If the size is much more, give a warning. [[BR]] (say 3 times or depending on the given size, e.g. for 1-2: 32 times, for 3-16: 8 times, for 17-32: 4 times, for 33-infinite: 3 times..)[[BR]] [[BR]] So we can have this code: {{{ #!cpp #include #include class x; // Class declaration, Incomplete type! class y; // Class declaration, Incomplete type! class z { public: z(char) {} }; // Full type class a { public: a(); ~a(); boost::optional2 x_; // incomplete type boost::optional2 y_; // incomplete type boost::optional2 z_; // sizeof(T) like in old boost::optional }; class x { public: x(int) {} }; class y { public: y(double) {} char c[5]; }; a::a() { // Call constructors in lazy way x_ = boost::in_place(1); y_ = boost::in_place(2.0); z_ = boost::in_place('c'); } a::~a() {} int main() { } }}} Full code resides here:[[BR]] http://pastebin.com/4db0RFC8 [[BR]] http://ideone.com/mBFVi [[BR]] [[BR]] In short, it just adds std::size_t N to the optional. And then chooses the right size in aligned_storage. {{{ #!cpp template class aligned_storage { // Borland ICEs if unnamed unions are used for this! union dummy_u_helper { char data[ N ]; } dummy_helper ; union dummy_u { char data[ N ]; BOOST_DEDUCED_TYPENAME type_with_alignment< ::boost::alignment_of::value >::type aligner_; } dummy_ ; public: void const* address() const { return &dummy_.data[0]; } void * address() { return &dummy_.data[0]; } } ; template class aligned_storage { // Borland ICEs if unnamed unions are used for this! union dummy_u { char data[ sizeof(T) ]; BOOST_DEDUCED_TYPENAME type_with_alignment< ::boost::alignment_of::value >::type aligner_; } dummy_ ; public: void const* address() const { return &dummy_.data[0]; } void * address() { return &dummy_.data[0]; } } ; }}} I have not added check in the link above but it is easy to add it using BOOST_STATIC_ASSERT and BOOST_STATIC_WARNING (which resides in boost/serialization/static_warning.hpp) Thanks !",Feature Requests,closed,To Be Determined,optional,Boost Development Trunk,Optimization,invalid,,