|
Boost Users : |
Subject: [Boost-users] [Serialization] Free-standing serialize function for "enum class"
From: Rüdiger Berlich (ruediger.berlich_at_[hidden])
Date: 2016-10-13 12:56:05
Hi there,
I have for a while suspected a problem with the serialization of an âenum classâ item (currently with Boost 1.61 on Ubuntu 16.04), and have written a small test program to further examine any potential problems. While the generated XML looks fine and I cannot see any problems with the enum class, my custom, free-standing âserializeâ function does not get called.
It is modelled after the examples for non-intrusive serialization, so I do not understand the reason behind this. Any idea ?
/*************************************/
// Compile with g++ --std=c++14 -o enum_ser -I/opt/boost/include/ -L/opt/boost/lib enum_ser.cpp -lboost_serialization
#include <boost/serialization/nvp.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <iostream>
#include <sstream>
enum class test_enum : int {a=5,b=6};
namespace boost {
namespace serialization {
//-----------------------------------------
template<typename Archive>
void serialize(Archive &ar , test_enum &x, const unsigned int){
std::cout << "Serializing via custom serialize function" << std::endl;
int t = 0;
if (Archive::is_saving::value) {
t = static_cast<int>(x);
ar & t;
} else {
ar & t;
x = static_cast<test_enum>(t);
}
}
//-----------------------------------------
}
}
int main(int argc, char **argv){
std::ostringstream oss;
test_enum x = test_enum::a;
{
boost::archive::xml_oarchive oa(oss);
oa << BOOST_SERIALIZATION_NVP(x);
}
std::cout << oss.str() << std::endl;
}
/*************************************/
Output of the program (note: the âSerializing via custom serialize functionâ does not appear) :
/*************************************/
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="14">
<x>5</x>
</boost_serialization>
/*************************************/
Thanks and Kind Regards,
Beet
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net