0

I am having troubles to use cereal with the PIMPL idiom.

This is a minimal example:

b.h

#ifndef _B_H_
#define _B_H_

#include <memory>
#include "cereal/types/memory.hpp"
#include "cereal/archives/json.hpp"

struct BImpl;

class B
{
public:
    B();
    ~B();

private:
    std::unique_ptr<BImpl> _impl;

    friend class cereal::access;

    template <class Archive>
    void serialize( Archive& ar )
    {
        ar( CEREAL_NVP( _impl ) );
    }
};

#endif

b.cpp

#include "b.h"

struct BImpl
{
     int b_i = 0;

private:
    friend class cereal::access;

    template <class Archive>
    void serialize( Archive & ar )
    {
        ar(
            CEREAL_NVP( b_i )
          );
    }
};

B::B() : _impl( new BImpl )
{
}

B::~B()
{
}

main.cpp

#include "b.h"
#include <fstream>
#include "cereal/archives/json.hpp"

using namespace std;

int main( int argc, char** argv )
{
    B b1;
    {
        std::ofstream file( "out.json" );
        cereal::JSONOutputArchive archive( file );
        archive( CEREAL_NVP( b1 ) );
    }
}

And here the errors that I get on MSVC 2015 Community Edition when I try to compile the minimal example:

  • C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(428): error C2139: 'BImpl': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_polymorphic'

  • C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\include\type_traits(435): error C2139: 'BImpl': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_abstract'

I am quite sure that I am not the first attempting to do this, but I have not been able to find nothing specific in the documentation or code snippets with a working solution.

4

1 回答 1

0

我按照此处描述的方法找到了一个可行的解决方案:http: //www.boost.org/doc/libs/1_46_1/libs/serialization/doc/pimpl.html

在实践中,我已经: * 将定义移动B::serializeB.cpp * 添加B.cpp到我使用的档案的所有不同实例中

这里是描述问题的票:https ://github.com/USCiLab/cereal/issues/324

于 2016-08-01T13:04:11.757 回答