Opened 12 years ago

Closed 12 years ago

#5172 closed Feature Requests (invalid)

Boost Optional enhancement

Reported by: nn-mail@… Owned by: Fernando Cacciola
Milestone: To Be Determined Component: optional
Version: Boost Development Trunk Severity: Optimization
Keywords: Cc:

Description

Boost.Optional is a very handy class.
It can be extended to allow more flexibility.
Proposal:
Add additional argument with size to optional.
Then the storage is created with the given size without checking sizeof(T).
And only in the construct function the size is being checked.
If the size is less than sizeof(T) , give error.
If the size is much more, give a warning.
(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..)

So we can have this code:

#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:
http://pastebin.com/4db0RFC8
http://ideone.com/mBFVi

In short, it just adds std::size_t N to the optional. And then chooses the right size in aligned_storage.

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 !

Attachments (1)

optional2.h (31.0 KB ) - added by nn-mail@… 12 years ago.
Optional proposal file. In case links don't work.

Download all attachments as: .zip

Change History (3)

by nn-mail@…, 12 years ago

Attachment: optional2.h added

Optional proposal file. In case links don't work.

comment:1 by Steven Watanabe, 12 years ago

This shouldn't be part of optional. It's functionality is orthogonal to that of optional.

// Allows storing T, but does not require T to be
// a complete type except in construct/copy/destroy operations.
// Requires: size >= sizeof(T)
//           size % alignment == 0
//           alignment % alignment_of<T>::value == 0
template<class T, std::size_t size, std::size_t alignment>
class typed_buffer {
public:
    template<class T...>
    typed_buffer(T&& t...);
    ~typed_buffer();
    typed_buffer& operator=(const typed_buffer&);
    T& get();
    const T& get() const;
private:
    boost::aligned_storage<size, alignment> _storage;
};

comment:2 by Steven Watanabe, 12 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.