Q:

serialization of class with const members using Boost

Consider the following code snippet
class tmp1
{
    const int a_;
    const double b_;   
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int ver)
    {
        ar & a_ & b_ ;
    }

public:
    tmp1(const itype a , const ftype b) : a_(a), b_(b)
    {}
};

I can write the object to a file, by doing
tmp1 t1(2, 10.0);    
std::string filename ="D:/Temp/demofile.txt";
std::ofstream ofs(filename);    
boost::archive::text_oarchive oa(ofs);
oa<<t1;

I would like to construct another instance of tmp1 by reading the file. Ideally I would like this to happen in a second constructor, that takes the file name and constructs it. How Do I accomplish this?
I tried 
tmp1 t2(10, 100.0);
    std::ifstream ifs(filename);
boost::archive::text_iarchive ia(ifs);
ia>>t2;

but VS2012 compilation fails with following message
archive/detail/check.hpp(162): error C2338: typex::value
4>          \boost\boost_1_67_0\boost/archive/detail/iserializer.hpp(611) : see reference to function template instantiation 'void boost::archive::detail::check_const_loading<T>(void)' being compiled
4>          with
4>          [
4>              T=const itype
4>          ]

which I assume due to members being const. I thought boost would cast away the const qualifier but seems not the case.

A:

What you're looking for is "non-default constructors" in the documentation:
https://www.boost.org/doc/libs/1_67_0/libs/serialization/doc/index.html
You need to write an overload for
template<class Archive, class T>
void load_construct_data(
    Archive & ar, T * t, const unsigned int file_version
);

so for class Foo, which for example is constructed with an integer and a string, you would provide:
template<class Archive>
void load_construct_data(
    Archive & ar, Foo * t, const unsigned int file_version
)
{
    int a;
    std::string b;
    ar >> a >> b;
    new (t) Foo(a, std::move(b));
}

