blitzpp / blitz

Blitz++ Multi-Dimensional Array Library for C++
https://github.com/blitzpp/blitz/wiki
Other
406 stars 84 forks source link

Fix for boost serialization #34

Closed efp closed 4 years ago

efp commented 6 years ago

When compiling with boost serialization versions 1.60.0 and up, one will get an error that boost can't access the memblock destructor. This adds the required friend statement for boost.

slayoo commented 6 years ago

Thank you! Let me keep it open for a few days and add a new Boost example to the continuous integration build beforehand so the fix would then also be exemplified.

efp commented 6 years ago

I tried this program

#define BZ_HAVE_BOOST_SERIALIZATION

#include <blitz/array.h>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>

#include <iostream>
#include <sstream>

int main()
{
    std::stringstream ss ;

    blitz::TinyVector<int, 3> dims{1,1,1} ;
    blitz::Array<float, 3> array(dims) ;
    array(0,1,0) = 3 ;

    boost::archive::text_oarchive oa(ss);
    oa << array ;

    std::cout << ss.str() << std::endl ;

    ss.seekg(0) ;
    blitz::Array<float, 3> array2(dims) ;    
    boost::archive::text_iarchive ia(ss);

    ia >> array2 ;

    std::cout << array2(0,1,0) << std::endl ;
}

On Ubuntu 18.04 gcc 7.3.0

g++ -o bstest -I. -lblitz bstest.cpp -lboost_serialization

Before fix:

/usr/local/include/boost/archive/detail/iserializer.hpp:246:17: error: ‘blitz::MemoryBlock<P_type>::~MemoryBlock() [with P_type = float]’ is protected within this context

After the fix, builds and runs with output:

22 serialization::archive 15 0 0 0 0 2 1 0
0 1 0.000000000e+00 0 0 0 3 1 1 1 0 0 0 0 0 3 1 1 1 3 2 1 0 3 0 0 0 0 0 3 1 1 1 0
0

Ok, that last number isn't right... I never tried it with a stringstream before.

Oddly, it fails to link with boost serialization unless I put the -lboost_serialization after the cpp file name.

Also note, the error only occurs when using the input archive; I tried it first with only the output archive and it compiled.

slayoo commented 6 years ago

thank you!

citibeth commented 5 years ago

@slayoo Can we merge this?

slayoo commented 5 years ago

As above, I vote for adding at least a minimalistic test case

efp commented 4 years ago

Since this fixes a compilation error, it doesn't seem amenable for addition to the test suite.

slayoo commented 4 years ago

Sorry, here's a PR that should hopefully fix the merge conflict: https://github.com/blitzpp/blitz/pull/165

slayoo commented 4 years ago

merged, thank you!