c++17 引入了新类型std::variant
. 是否可以定义一个序列化例程,以便与std::variant
结合使用boost::mpi
?
例如,考虑一个简单的程序
#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;
}
它不编译给出错误
error: ‘class std::variant<A, B>’ has no member named ‘serialize’
如何正确定义 a 的serialize
成员std::variant
?请注意,在上面的示例中,这两种类型A
都有B
一个正确定义的序列化成员。