【问题标题】:Is it possible to use boost::serialization with managed class?是否可以将 boost::serialization 与托管类一起使用?
【发布时间】:2011-05-03 01:04:27
【问题描述】:

我们有很多使用 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;
};

这里是主要的测试代码:

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;
}

是否可以将 boost::serialization 与托管类一起使用?

编辑:

如果我们把类的使用代码改成这样:

    ...
    gps_position2^ g = gcnew gps_position2(24.567f);
    ...

那么我们只得到 1 个错误:

error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE&lt;x&gt;' D:\path\to\Boost\boostw\boost\archive\detail\check.hpp 60

【问题讨论】:

  • 删除属性后问题是否仍然存在(但该类仍然是 ref 类)?
  • 你能改用.NET序列化吗? C++ 和 c++-cli 不是同一种语言。尽管它们可以在某种程度上混合,但 boost 是针对 C++ 的,而不是 C++-cli。
  • @CiscolPPhone:是的,它仍然存在。 @Merlyn Morgan-Graham:不,我们必须在主代码中使用 boost:serialization(在本机 C++ 中),这段代码只是主程序的一个工具。
  • 什么版本的boost?在 check.hpp 的那一行中可能有描述。您可能还想在news.gmane.org/gmane.comp.lib.boost.user 尝试您的问题。
  • 谢谢doomdayx,但我已经在那里问过了,但没有得到明确的答案(只有这条评论:“未知他们是否可以使用托管类,但确实知道托管C++有一个很多模板问题,通常不适用于“真正的”C++ 代码。”)关于版本:我们在这里使用 boost 1.42。

标签: visual-c++ boost c++-cli managed-c++ boost-serialization


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-07-21
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多