1

I am trying to use the default serialization facilities with boost, but I didn't manage to serialize a complex vector. I tried this:

#include <fstream>
#include <complex>
#include <vector>

#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/complex.hpp>

using namespace std;
typedef complex<double> type;

template<class Archive>
void serialize(Archive& ar, vector<type>& objs, const unsigned version) {
    ar & objs;
}

int main(){
    vector<type> r;
    r.push_back(1.3);
    r.push_back(12.12);
    r.push_back(-31.05);

    ofstream ofs("test.txt");
    boost::archive::text_oarchive oa(ofs);
    oa & r;
    return 0;
}

which compiles fine but crashes on execution. Yet if I do

typedef double type

then everything works OK. I can't figure out a solution for that. Any help?

Thanks,

Jan

7
  • Please do not do that "#define type complex<double>" - better is typedef complex<double> type;
    – user2249683
    Oct 8, 2013 at 12:39
  • @DieterLücking You are right of course, but I just threw a fast code to put some emphasis on the difference between the behaviour for double and complex<double>. Somehow, it looked more explicit this way when I wrote it - now I don't know why I thought that.
    – legrojan
    Oct 8, 2013 at 16:52
  • Btw: the code works fine. The output file contains: "22 serialization::archive 9 0 0 3 0 1.3 0 12.119999999999999 0 -31.050000000000001 0"
    – user2249683
    Oct 8, 2013 at 16:59
  • Hmmmm... in that case it may have to do with my setup. I use a pre-compiled version of boost for VS 2012. I guess that's a no-no, I will have to compile it myself. Thanks!
    – legrojan
    Oct 8, 2013 at 19:43
  • Well, I thought that was it, however I tried on a different computer, where I compiled boost myself, and all I get is an empty file. So I wonder, @DieterLücking, did you compile my test code with Visual C++ 2012, or was it something else, perhaps GCC? Because it could be a boost bug.
    – legrojan
    Oct 10, 2013 at 5:43

2 Answers 2

0

Just to wrap up and mark as answered:

The problem seems to be with the boost 1.54 implementation for Visual Studio 2012, since the g++ version of the code is reported to work correctly. Thanks to @DieterLücking for testing my code and pointing me in the right direction.

0

lol - rather than saying:

The problem seems to be with the boost 1.54 implementation for Visual Studio 2012, since the g++ version of the code is reported to work correctly.

I would say:

The problem seems to be with Visual Studio 2012, since the g++ version of the code is reported to work correctly.

Rbert Ramey

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.