Boost logo

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);

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


Thanks and Kind Regards,

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at