Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#13478 closed Bugs (fixed)

multi_index_container serialization of non copyable types

Reported by: Sébastien Paris <sebastien.paris@…> Owned by: Joaquín M López Muñoz
Milestone: To Be Determined Component: multi_index
Version: Boost 1.66.0 Severity: Problem
Keywords: serialization non-copyable Cc:

Description

A multi_index container holding non-copyable types fails to serialize because of delayed insertion through std::pair<node_type*,bool> insert_(const Value& v,Variant variant) in multi_index_container.hpp:555 happening in void load(Archive& ar,const unsigned int version) in multi_index_container.hpp:940

The insertion should use a rvalue reference overload moving the previously loaded value.

Minimal example:

template<class Archive>
void check_serialize(Archive &ar)
{
    using non_copyable_type = std::unique_ptr<int>;
    using non_copyable_multi_index = boost::multi_index_container
        <   non_copyable_type
        ,   boost::multi_index::indexed_by
            <   boost::multi_index::hashed_unique
                <   boost::multi_index::const_mem_fun
                    <   non_copyable_type
                    ,   typename non_copyable_type::pointer
                    ,   &non_copyable_type::get
                    >
                >
            >
        >;

   non_copyable_multi_index mu; // fine
   
   if constexpr (Archive::is_saving::value)
        ar << boost::serialization::make_nvp("mu",mu); // fine
   else
        ar >> boost::serialization::make_nvp("mu",mu); // error
}

Change History (3)

comment:1 by Sébastien Paris <sebastien.paris@…>, 5 years ago

Proposed resolution: modify multi_index_container.hpp:954-955 from

      std::pair<node_type*,bool> p=insert_(
        value.get(),super::end().get_node());

to

      std::pair<node_type*,bool> p=insert_(
        value.get(),super::end().get_node(),detail::rvalue_tag());

comment:2 by Joaquín M López Muñoz, 5 years ago

Hi Sébastien,

Thank you for the bug report. Also, your proposed fix is correct.

It's going to take me a couple of days till I find the time to change this in develop. As we're now closing Boost 1.67, the change will go in Boost 1.68.

Best regards,

Last edited 5 years ago by Joaquín M López Muñoz (previous) (diff)

comment:3 by Joaquín M López Muñoz, 5 years ago

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