模板类的非侵入式序列化方法
我正在使用 boost 序列化,主要是侵入式版本。对于模板 Matrix 类,我想要 非侵入式版本,适用于 Visual Studio,代码如下:
namespace boost
{
namespace serialization
{
template<class Archive, int R, int C, class ElementType>
void serialize(Archive & ar, Matrix<R, C, ElementType> & m, const unsigned int version)
{
ar & ...
}
}
}
int R
、int C
是行和列、ElementType
> 是双精度
或浮点
。
但是,这在 GCC 4.3.2 上不起作用,并出现错误
error: 'class Matrix<1u, 3u, double>' has no member named 'serialize'
If I use a Special form like
namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive & ar, Matrix<3,1,double> & m, const unsigned int version)
{
ar & ...
}
}
}
it compiles on GCC,但当然仅适用于一组特殊的模板参数。
我该怎么做才能使其在所有 R
、C
和 ElementType
的两个编译器上运行?
编辑:这些是导致错误的行:
/[myfolder]/lib/BOOST/1_44_0/include/boost/serialization/access.hpp: In static member function 'static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::binary_iarchive, T = Matrix<3u, 1u, double>]':
/[myfolder]/lib/BOOST/1_44_0/include/boost/serialization/serialization.hpp:70: instantiated from 'void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::binary_iarchive, T = Matrix<3u, 1u, double>]'
/[myfolder]/lib/BOOST/1_44_0/include/boost/serialization/serialization.hpp:129: instantiated from 'void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::binary_iarchive, T = Matrix<3u, 1u, double>]'
/[myfolder]/lib/BOOST/1_44_0/include/boost/archive/detail/iserializer.hpp:182: instantiated from 'void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::binary_iarchive, T = Matrix<3u, 1u, double>]'
I am using boost serialization, mostly the intrusive version. For a template Matrix class I would like to have the non-intrusive version which works on Visual Studio with the following code:
namespace boost
{
namespace serialization
{
template<class Archive, int R, int C, class ElementType>
void serialize(Archive & ar, Matrix<R, C, ElementType> & m, const unsigned int version)
{
ar & ...
}
}
}
int R
, int C
are the row and columns, ElementType
is double
or float
.
However, this does not work on GCC 4.3.2 with the error
error: 'class Matrix<1u, 3u, double>' has no member named 'serialize'
If I use a special form like
namespace boost
{
namespace serialization
{
template<class Archive>
void serialize(Archive & ar, Matrix<3,1,double> & m, const unsigned int version)
{
ar & ...
}
}
}
it compiles on GCC, but of course only for a special set of template arguments.
What can I do to make it work on both compilers for all R
, C
and ElementType
?
EDIT: These are the lines causing the error:
/[myfolder]/lib/BOOST/1_44_0/include/boost/serialization/access.hpp: In static member function 'static void boost::serialization::access::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::binary_iarchive, T = Matrix<3u, 1u, double>]':
/[myfolder]/lib/BOOST/1_44_0/include/boost/serialization/serialization.hpp:70: instantiated from 'void boost::serialization::serialize(Archive&, T&, unsigned int) [with Archive = boost::archive::binary_iarchive, T = Matrix<3u, 1u, double>]'
/[myfolder]/lib/BOOST/1_44_0/include/boost/serialization/serialization.hpp:129: instantiated from 'void boost::serialization::serialize_adl(Archive&, T&, unsigned int) [with Archive = boost::archive::binary_iarchive, T = Matrix<3u, 1u, double>]'
/[myfolder]/lib/BOOST/1_44_0/include/boost/archive/detail/iserializer.hpp:182: instantiated from 'void boost::archive::detail::iserializer<Archive, T>::load_object_data(boost::archive::detail::basic_iarchive&, void*, unsigned int) const [with Archive = boost::archive::binary_iarchive, T = Matrix<3u, 1u, double>]'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对我来说,这看起来像是有符号/无符号的不匹配。您的模板函数是用
int
声明的,但错误表明它尝试与模板匹配的对象具有参数1u
和3u
。当您实例化尝试序列化的对象时,您是否使用无符号值作为维度?尝试更改您的序列化模板函数以采用unsigned
或使用int
实例化您的 Matrix。It looks like a signed/unsigned mismatch to me. Your template function is declared with
int
s but the error indicates that the object which it's trying to match with the template has parameters1u
and3u
. When you instantiate the object that you're trying to serialize, are you using unsigned values for the dimensions? Try changing your serialize template function to takeunsigned
s or instantiating your Matrix withint
s.