Boost logo

Boost :

From: Robert Ramey (ramey_at_[hidden])
Date: 2004-04-12 14:13:51


Joaqumn M Lspez Muqoz wrote:

> push_back() can also throw from the allocator if it runs out of memory,
> so you really need some proper clenaup regardless of the guarantees
> made by copying ops. Dave's suggestion of using RAII is probably the
> most elegant way to deal with the situation: in my experience, however,
> this sort of scope guards perform worse than a try{}catch(...){}. Your
> mileage may vary.

Anything wrong with the following?

// reserve space on stack for an object of type T without actually
// construction such an object
template< typename T >
struct stack_allocate
{
    T * address() {
        return static_cast<T*>(storage_.address());
    }
    T & reference() {
        return * address();
    }
private:
    aligned_storage< sizeof(T), alignment_of<T>::value > storage_;
};

// construct element on the stack
template<class Archive, class T>
struct stack_construct : public stack_allocate<T>
{
        stack_construct(Archive & ar){
        load_construct_data(ar, address(), 0U);
        }
        ~stack_construct(){
        address()->~T(); // undo load_construct_data above
        }
};

// sequential container input
template<class Archive, class Container>
struct archive_input_seq
{
// archive_input_seq(){}
    inline void operator()(Archive &ar, Container &s)
    {
        typedef BOOST_DEDUCED_TYPENAME Container::value_type type;
        stack_construct<Archive, type> t(ar);
        ar >> make_nvp("item", t.reference());
        s.push_back(t.reference());
    }
};


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk