是否可以对托管类使用boost :: serialization?

Say*_*yid 7 boost managed-c++ c++-cli boost-serialization visual-c++

我们有很多使用boost :: serialization完美序列化的本机c ++类.

现在我们想将一些成员字段更改为property,因此我们可以在PropertyGrids中使用它们.当我们将类定义更改为ref class X时,我们得到了大量的编译错误:

#1: error C2893: Failed to specialize function template 'boost::archive::text_oarchive &boost::archive::detail::interface_oarchive<Archive>::operator <<(T &)' d:\someAddress\someFile.cpp 58

#2: error C2784: 'std::basic_ostream<_Elem,_Traits> &std::operator <<(std::basic_ostream<_Elem,_Traits> &,const std::_Smanip<_Arg> &)' : could not deduce template argument for 'std::basic_ostream<_Elem,_Traits> &' from 'boost::archive::text_oarchive' d:\someAddress\someFile.cpp 58

我们这里有很多小类,所以为每个类编写一个包装器会很麻烦!

这是我们使用的示例类:

ref class gps_position2
{
public:
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & seconds;
    }
public:
    gps_position(){};
    gps_position(float s)
    {
        this->seconds = s;
    }

    property float seconds;
};
Run Code Online (Sandbox Code Playgroud)

这是主要的测试代码:

int main()
{
    std::ofstream ofs("out.txt");

    gps_position2 g(24.567f);

    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << g;
    }
    // ................
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

甚至可以将boost :: serialization与托管类一起使用吗?

编辑:

如果我们将类使用代码更改为:

    ...
    gps_position2^ g = gcnew gps_position2(24.567f);
    ...
Run Code Online (Sandbox Code Playgroud)

然后我们只得到1个错误:

error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>' D:\path\to\Boost\boostw\boost\archive\detail\check.hpp 60

Set*_*eth 0

我知道你说过你不想结束所有课程。然而,与其将所有类包装在 C++/CLI 中,不如开发一个非托管 C++ dll 并公开所有相关函数。然后,您可以使用P/Invoke从托管代码(例如 C++/CLI)调用非托管 dll。不确定这是否可行。