[Boost-bugs] [Boost C++ Libraries] #5172: Boost Optional enhancement

Subject: [Boost-bugs] [Boost C++ Libraries] #5172: Boost Optional enhancement
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-02-09 12:32:33


#5172: Boost Optional enhancement
-------------------------------------+--------------------------------------
 Reporter: nn-mail@… | Owner: fcacciola
     Type: Feature Requests | Status: new
Milestone: To Be Determined | Component: optional
  Version: Boost Development Trunk | Severity: Optimization
 Keywords: |
-------------------------------------+--------------------------------------
 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 <boost/optional2.hpp>
 #include <boost/utility/in_place_factory.hpp>

 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, 10> x_; // incomplete type
     boost::optional2<y, 20> y_; // incomplete type
     boost::optional2<z> 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 T, std::size_t N>
 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<dummy_u_helper>::value
>::type aligner_;
         } dummy_ ;



 public:

         void const* address() const { return &dummy_.data[0]; }
         void * address() { return &dummy_.data[0]; }
 } ;

 template <class T>
 class aligned_storage<T, 0>
 {
         // 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<T>::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 !

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5172>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:05 UTC