serializing an std::chrono::duration object with Boost
-
Why is it that in the following program, the value of newCount that prints at the end is 10, as I would expect, while the value of newTime.count() that prints is -3689348814741910324?
#include "stdafx.h"
#include
#include
#include
#include
#includenamespace boost
{
namespace serialization
{
template
void serialize(Archive&ar, std::chrono::nanoseconds&time, const unsigned int version)
{
long long count{ time.count() };
ar&count;
}
}
}int main()
{
std::chrono::nanoseconds time(10);
long long count = time.count();
{
std::ofstream saveFile("filename");
boost::archive::text_oarchive boostOutputArchieve(saveFile);
boostOutputArchieve << time;
boostOutputArchieve << count;
}
std::chrono::nanoseconds newTime;
long long newCount;
{
std::ifstream loadFile("filename");
boost::archive::text_iarchive boostInputArchieve(loadFile);
boostInputArchieve >> newTime;
boostInputArchieve >> newCount;
}
std::cout << newTime.count() << '\n' << newCount << '\n';
return 0;
}I would have thought setting up the serialize() overload for nanoseconds to serialize the tick count and using that would have the same result as serializing the tick count directly.
-
Why is it that in the following program, the value of newCount that prints at the end is 10, as I would expect, while the value of newTime.count() that prints is -3689348814741910324?
#include "stdafx.h"
#include
#include
#include
#include
#includenamespace boost
{
namespace serialization
{
template
void serialize(Archive&ar, std::chrono::nanoseconds&time, const unsigned int version)
{
long long count{ time.count() };
ar&count;
}
}
}int main()
{
std::chrono::nanoseconds time(10);
long long count = time.count();
{
std::ofstream saveFile("filename");
boost::archive::text_oarchive boostOutputArchieve(saveFile);
boostOutputArchieve << time;
boostOutputArchieve << count;
}
std::chrono::nanoseconds newTime;
long long newCount;
{
std::ifstream loadFile("filename");
boost::archive::text_iarchive boostInputArchieve(loadFile);
boostInputArchieve >> newTime;
boostInputArchieve >> newCount;
}
std::cout << newTime.count() << '\n' << newCount << '\n';
return 0;
}I would have thought setting up the serialize() overload for nanoseconds to serialize the tick count and using that would have the same result as serializing the tick count directly.
I've realized that -3689348814741910324 is the value uninitialized variables get set to when debugging, so...does that mean that the line
boostInputArchieve >> newTime;
is currently doing nothing?
-
I've realized that -3689348814741910324 is the value uninitialized variables get set to when debugging, so...does that mean that the line
boostInputArchieve >> newTime;
is currently doing nothing?
Can't you debug this code? :confused:
-
I've realized that -3689348814741910324 is the value uninitialized variables get set to when debugging, so...does that mean that the line
boostInputArchieve >> newTime;
is currently doing nothing?
Apparently the fact that the serialization requires (as far as I can tell) the extra step of reading the count into a variable breaks the symmetry between save and load operations, so I have to overload them individually rather than handling both in a serialize() overload. This is what I've tried:
namespace boost
{
namespace serialization
{
template
void save(Archive&ar, const std::chrono::nanoseconds&time, const unsigned int version)
{
long long count{ time.count() };
ar&count;
}
template
void load(Archive&ar, std::chrono::nanoseconds&time, const unsigned int version)
{
long long count;
ar&count;
time = std::chrono::nanoseconds(count);
}
}
}BOOST_SERIALIZATION_SPLIT_FREE(std::chrono::nanoseconds)
It seems to work, though I'm not confident that this is the way it's supposed to be done.
-
Can't you debug this code? :confused:
If I knew what I was looking for. It's entirely possible that my debugging skills are lacking.