我正在尝试为我们的代码库启用 C++17,该代码库强烈基于 boost - 和 boost::serialization 用于中间数据存储和预传输序列化。
总的来说,一切看起来都很好,似乎工作正常,除了我们序列化 Eigen::Matrix 对象 时。和 包括用于共享 ptr 序列化的 boost 序列化支持 header 。
github上的最小示例/测试代码:https://github.com/nightsparc/EigenSerialize
[编辑] @Marc Glisse 在下面提供了一个简化的测试用例。见:https://stackoverflow.com/a/54536756/1267320
我用不同的编译器(GCC6/7/8 和 Clang6)做了一些测试。我们通常使用系统 GCC,即 Ubuntu 18.04 的 GCC7.3。
对我来说,这似乎是与 GCC7 及更高版本的 C++17 模式有关的问题。
我的意思是,我没有在最小示例中使用 shared_ptr,所以我可以删除它,一切都会好起来的……尽管如此,在我们的代码库中,shared_ptr 到处都被序列化了。
你们当中有人知道这里发生了什么吗?还是 GCC C++17 模式中的错误?
测试代码(没有适当的错误处理和东西......):
#include <fstream>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/split_free.hpp>
#include <Eigen/Core>
// !! Conflicting include! Whenever the serialization wrapper for shared_ptrs is included
// the compilation fails!
// /usr/local/include/Eigen/src/Core/util/ForwardDeclarations.h:32:
// error: incomplete type ‘Eigen::internal::traits<boost::serialization::U>’ used in nested name specifier
// enum { has_direct_access = (traits<Derived>::Flags & DirectAccessBit) ? 1 : 0,
#include <boost/serialization/shared_ptr.hpp>
// Serialization methods for fixed-size Eigen::Matrix type
namespace boost {
namespace serialization {
template<
class Archive,
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols
>
inline void serialize(Archive & arArchive,
Eigen::Matrix<_Scalar,
_Rows,
_Cols,
_Options,
_MaxRows,
_MaxCols> & arMatrix,
const unsigned int aVersion)
{
boost::serialization::split_free(arArchive, arMatrix, aVersion);
}
template<
class Archive,
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols
>
inline void save(Archive & arArchive,
const Eigen::Matrix<_Scalar,
_Rows,
_Cols,
_Options,
_MaxRows,
_MaxCols> & arMatrix,
const unsigned int)
{
typedef typename Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Index TEigenIndex;
const TEigenIndex lRows = arMatrix.rows();
const TEigenIndex lCols = arMatrix.cols();
arArchive << lRows;
arArchive << lCols;
if(lRows > 0 && lCols > 0)
{
arArchive & boost::serialization::make_array(arMatrix.data(), arMatrix.size());
}
}
template<
class Archive,
typename _Scalar,
int _Rows,
int _Cols,
int _Options,
int _MaxRows,
int _MaxCols
>
inline void load(Archive & arArchive,
Eigen::Matrix<_Scalar,
_Rows,
_Cols,
_Options,
_MaxRows,
_MaxCols> & arMatrix,
const unsigned int)
{
typedef typename Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::Index TEigenIndex;
TEigenIndex lRows, lCols;
// deserialize meta data
arArchive & lRows;
arArchive & lCols;
// do some error handling here
if(lRows > 0 && lCols > 0)
{
// deserialize data
arArchive & boost::serialization::make_array(arMatrix.data(), arMatrix.size());
}
}
}
}
class TestClass
{
public:
TestClass()
{
// fill eigen
m(0,0) = 3;
m(1,0) = 2.5;
m(0,1) = -1;
m(1,1) = m(1,0) + m(0,1);
}
private:
friend class boost::serialization::access;
Eigen::Matrix2d m;
template<class Archive>
void serialize(Archive &ar, const unsigned int)
{
ar & m;
}
};
int main(void)
{
using namespace boost::archive;
// Serialize
TestClass TestA;
std::ofstream oss("test.log");
{
text_oarchive oa(oss);
oa << TestA;
}
// deserialize now
TestClass TestB;
std::ifstream iss("test.log");
{
text_iarchive ia(iss);
ia >> TestB;
}
}
[编辑 2019-02-06]
GCC 错误:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84075
特征错误:http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1676
[编辑 2019-02-07]
boost 公关:https://github.com/boostorg/serialization/pull/144
最佳答案
我猜(不可靠)C++ 14 和 C++ 17 之间的差异是由于模板参数推导(编译器不能仅仅因为参数数量太少而关闭模板),gcc 不能处理为SFINAE。我不知道这个错误是在 gcc 还是 Eigen 中,但无论如何这里有一个更精简的测试用例
#include <Eigen/Core>
template<template<class U>class SPT>void f(SPT<class U>&);
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
void f(Eigen::Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & arMatrix){}
int main()
{
Eigen::Matrix2d m;
f(m);
}
关于c++ - 特征矩阵 + Boost::序列化/C++17,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54534047/