As I said in a comment. Yes, there are no problems with building from the archive. (Another alternative is the static load
function, but this can lead to performance penalties).
The only potential problem that I see with your approach is that your constructor can take almost anything as an argument and can cause problems. And this can interfere with the copy constructor and other single argument constructors that rely on implicit conversion.
Thus, you need to limit yourself to archiving.
There are different methods for this, but based on this talk http://marc.info/?l=boost&m=121131260728308&w=2 and the fact that the archives
inheritance tree is documented http://www.boost.org/doc/libs /1_35_0/libs/serialization/doc/class_diagram.html , I think that the best solution is to check that the argument is derived from basic_iarchive
.
#include<type_traits> struct Foo { ... std::vector<int> data; Foo() { // populate "data" by doing calculation data.push_back(1); data.push_back(2); } template<class IArchive, typename = std::enable_if_t<std::is_base_of<boost::archive::detail::basic_iarchive, IArchive>::value>> Foo( IArchive & ar ) { ar >> data; // populate "data" by reading the archive } ... }; int main(int argc, const char *argv[]) { // deserialize boost::archive::text_iarchive iar(std::cin); Foo foo(iar); // will also work with other archives }
As for what happens when your data is not constructive by default, see the discussion above.
source share