c++11 - serialize temporary into boost archive -
the following not possible boost output archive:
int foo(){ return 4; } ar << static_cast<unsigned int>(foo());
is there alternative without out creating local temporary x=foo()
.
and why underlying archive operator <<(t & t)
not const reference , output archive such above work?
this seems work, , think this why:
... detect such cases, output archive operators expect passed const reference arguments.
it seems worth noting in example ar << foo();
not work either (i.e. doesn't have cast).
#include <fstream> #include <iostream> #include <boost/serialization/serialization.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> unsigned int foo(){ return 4; } int main() { { std::ofstream outputstream("somefile.txt"); boost::archive::text_oarchive outputarchive(outputstream); outputarchive << static_cast<const int&>(foo()); } std::ifstream inputstream("somefile.txt"); boost::archive::text_iarchive inputarchive(inputstream); int readback; inputarchive >> readback; std::cout << "read back: " << readback << std::endl; return 0; }
Comments
Post a Comment