为什么我在使用textarchive时需要设置boost :: archive :: no_codecvt?

时间:2016-05-23 14:35:01

标签: boost archive

我们有一个序列化为boost :: archive的类;但它会在存档开头之前插入一个7个字符的字符串。

由于从1.56升级到升级1.61,这已经开始失败。

我们发现更换了这行:

boost::archive::polymorphic_text_iarchive ia(ifs);

boost::archive::polymorphic_text_iarchive ia(ifs, boost::archive::no_codecvt);

解决了这个问题。但很高兴知道为什么这已经开始在1.56和1.61之间失败,如果这是解决问题的正确方法。

// boost_archiving.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <iostream>
#include <sstream>
#include <fstream>

#include <boost/exception/exception.hpp>
#include <boost/exception/diagnostic_information.hpp>

#include <boost/archive/polymorphic_text_iarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>

struct SimpleStruct
{
    int x;
    int y;
    std::string string;
    double dub;

    template<typename T_ARCHIVE>

    void serialize(T_ARCHIVE &a, unsigned int version)
    {
        a & x;
        a & y;
        a & string;
        a & dub;
    }
};

typedef boost::array<char, 7> FileId;
const FileId FILE_ID = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };

void writeFile(const std::string& filename)
{
    std::ofstream ofs(filename.c_str());
    ofs.write(FILE_ID.data(), FILE_ID.size());
    boost::archive::text_oarchive oa(ofs);

    SimpleStruct simple;
    simple.x = 10;
    simple.y = 7;
    simple.string = "test";
    simple.dub = 2.5;

    oa << simple;
}


void readFile(const std::string& filename)
{
    std::ifstream ifs(filename.c_str());

    FileId fileId;
    ifs.read(fileId.data(), fileId.size());
    if (ifs.good() && (fileId == FILE_ID))
    {
        boost::archive::polymorphic_text_iarchive ia(ifs); // FAILS on 1.61 Swap this with the line below, or downgrade to boost 1.56
        // boost::archive::polymorphic_text_iarchive ia(ifs, boost::archive::no_codecvt); 

        SimpleStruct simple;
        ia >> simple;

        std::cout
            << simple.x << " "
            << simple.y << " "
            << simple.string << " "
            << simple.dub << std::endl;
    }
}

void main(int argc, char* argv[])
{
    try
    {
        std::string filename("archive.arch");
        writeFile(filename);
        readFile(filename);
    }
    catch (const std::exception& e)
    {
        std::cerr << boost::current_exception_diagnostic_information() << std::endl;
    }
}

示例存档:

abcdefg22 serialization::archive 11 0 0 10 7 4 test 2.50000000000000000e+000

0 个答案:

没有答案