模板类的非侵入式序列化方法

发布于 2024-10-27 11:59:52 字数 2387 浏览 1 评论 0原文

我正在使用 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 Rint 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,但当然仅适用于一组特殊的模板参数。

我该怎么做才能使其在所有 RCElementType 的两个编译器上运行?

编辑:这些是导致错误的行:

/[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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鯉魚旗 2024-11-03 11:59:52

对我来说,这看起来像是有符号/无符号的不匹配。您的模板函数是用 int 声明的,但错误表明它尝试与模板匹配的对象具有参数 1u3u。当您实例化尝试序列化的对象时,您是否使用无符号值作为维度?尝试更改您的序列化模板函数以采用 unsigned 或使用 int 实例化您的 Matrix。

It looks like a signed/unsigned mismatch to me. Your template function is declared with ints but the error indicates that the object which it's trying to match with the template has parameters 1u and 3u. 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 take unsigneds or instantiating your Matrix with ints.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文