[Boost-users] [Serialization] Free-standing serialize function for "enum class"

136 views
Skip to first unread message

Rüdiger Berlich

unread,
Oct 13, 2016, 12:56:19 PM10/13/16
to boost...@lists.boost.org
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 mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Bjorn Reese

unread,
Oct 13, 2016, 1:28:22 PM10/13/16
to boost...@lists.boost.org
On 10/13/2016 06:56 PM, Rüdiger Berlich wrote:

> 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.

Boost.Serialization automatically converts an enum into an int before
it is serialized.

Rüdiger Berlich

unread,
Oct 13, 2016, 1:37:35 PM10/13/16
to boost...@lists.boost.org
Hi Björn,



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.

Boost.Serialization automatically converts an enum into an int before
it is serialized.



so what you are saying is that in

oa << BOOST_SERIALIZATION_NVP(x);

x is first converted to an int, and as there no longer is a test_enum to be serialized, my own serialize()-function never gets called ?

Thanks for the hint and Kind Regards,
Beet

Reply all
Reply to author
Forward
0 new messages