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
#include
#include
#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(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>
<x>5</x>
/*************************************/
Thanks and Kind Regards,
Beet