2

c++17 introduces the new type std::variant. Is it possible to define a serialization routine, so as to use std::variant in conjunction with boost::mpi?

Consider, e.g., a simple program

#include <variant>
#include <boost/mpi.hpp>
#include <boost/serialization/string.hpp>

namespace mpi = boost::mpi;

class A {
  friend class boost::serialization::access;

  template <class Archive>
  void serialize(Archive& ar, const unsigned int version)
  {
    ar & x;
    ar & y;
  }

public:
  int x, y;
};

class B {
  friend class boost::serialization::access;

  template <class Archive>
  void serialize(Archive& ar, const unsigned int version)
  {
    ar & z;
  }

public:
  int z;
};

int main()
{
  mpi::environment env;
  mpi::communicator world;

  std::variant<A, B> v;
  if (world.rank() == 0)
    v = B{1};
  mpi::broadcast(world, v, 0);

  return 0;
}

It does not compile giving error

error: ‘class std::variant<A, B>’ has no member named ‘serialize’

How can one properly define a serialize member for a std::variant? Note that both types A and B in the example above have a correctly defined serialization member.

francesco
  • 7,189
  • 7
  • 22
  • 49

0 Answers0