Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions include/boost/serialization/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
#include <boost/serialization/split_free.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/detail/stack_constructor.hpp>

// use visitor from boost::variant
template<class Visitor, BOOST_VARIANT_ENUM_PARAMS(class T)>
Expand Down Expand Up @@ -159,11 +160,15 @@ struct variant_impl {
// with an implementation that de-serialized to the address of the
// aligned storage included in the variant.
typedef typename mpl::front<S>::type head_type;
head_type value;
ar >> BOOST_SERIALIZATION_NVP(value);
v = std::move(value);;
// The alternative is built the way a container element is,
// rather than declared, so that a type whose default
// constructor is private, or missing altogether, can be
// reached through boost::serialization::access.
detail::stack_construct<Archive, head_type> value(ar, version);
ar >> boost::serialization::make_nvp("value", value.reference());
v = std::move(value.reference());
head_type * new_address = & get<head_type>(v);
ar.reset_object_address(new_address, & value);
ar.reset_object_address(new_address, & value.reference());
return;
}
typedef typename mpl::pop_front<S>::type type;
Expand Down
53 changes: 53 additions & 0 deletions test/test_variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,57 @@ void test_reuse(const Variant & v){
std::remove(testfile);
}

// An alternative whose default constructor is private, reachable only
// through boost::serialization::access. Loading a variant has to build its
// alternative the way a container element is built, rather than declare one,
// or such a type cannot be an alternative at all. Reported by
// KJTsanaktsidis in
// https://github.com/boostorg/serialization/issues/338. Thanks!
class PD {
friend class boost::serialization::access;
PD() : m_x(0) {}
int m_x;
public:
explicit PD(int x) : m_x(x) {}
int value() const {
return m_x;
}
template<class Archive>
void serialize(Archive & ar, const unsigned int /* version */){
ar & boost::serialization::make_nvp("x", m_x);
}
};

int pd_value(const boost::variant<int, PD> & v){
return boost::get<PD>(v).value();
}

#ifndef BOOST_NO_CXX17_HDR_VARIANT
int pd_value(const std::variant<int, PD> & v){
return std::get<PD>(v).value();
}
#endif

template<class Variant>
void test_private_default_ctor(){
const char * testfile = boost::archive::tmpnam(NULL);
BOOST_REQUIRE(testfile != NULL);
const Variant v(PD(42));
{
test_ostream os(testfile, TEST_STREAM_FLAGS);
test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
oa << boost::serialization::make_nvp("v", v);
}
Variant v1;
{
test_istream is(testfile, TEST_STREAM_FLAGS);
test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
ia >> boost::serialization::make_nvp("v", v1);
}
BOOST_CHECK(42 == pd_value(v1));
std::remove(testfile);
}

int test_boost_variant(){
std::cerr << "Testing boost_variant\n";
boost::variant<bool, int, float, double, A, std::string> v;
Expand All @@ -229,6 +280,7 @@ int test_boost_variant(){
test_type(v1);
v = 1;
test_reuse(v);
test_private_default_ctor<boost::variant<int, PD> >();
return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -260,6 +312,7 @@ int test_std_variant(){
test_type(v1);
v = 1;
test_reuse(v);
test_private_default_ctor<std::variant<int, PD> >();
return EXIT_SUCCESS;
}
#endif
Expand Down