| 1 | #include "stdafx.h"
|
|---|
| 2 |
|
|---|
| 3 | struct X
|
|---|
| 4 | {
|
|---|
| 5 | int x;
|
|---|
| 6 |
|
|---|
| 7 | template<class Archive>
|
|---|
| 8 | void serialize(Archive & ar, const unsigned int /*file_version*/)
|
|---|
| 9 | {
|
|---|
| 10 | ar & x;
|
|---|
| 11 | }
|
|---|
| 12 | };
|
|---|
| 13 |
|
|---|
| 14 | BOOST_CLASS_IMPLEMENTATION(X, boost::serialization::object_serializable)
|
|---|
| 15 |
|
|---|
| 16 | template<class T>
|
|---|
| 17 | void BoostLoad(const char* fileName, T& obj)
|
|---|
| 18 | {
|
|---|
| 19 | std::ifstream ifs(fileName, std::ios_base::binary);
|
|---|
| 20 | boost::archive::binary_iarchive ia(ifs);
|
|---|
| 21 | ia >> obj;
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 | template<class T>
|
|---|
| 25 | void BoostSave(const char* fileName, T& obj)
|
|---|
| 26 | {
|
|---|
| 27 | std::ofstream ofs(fileName, std::ios_base::binary);
|
|---|
| 28 | boost::archive::binary_oarchive oa(ofs);
|
|---|
| 29 | oa << obj;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | int main()
|
|---|
| 33 | {
|
|---|
| 34 | // instructions:
|
|---|
| 35 | // 1) uncomment this and run to create the file
|
|---|
| 36 | // 2) comment this and run to see that x2 is loaded correctly
|
|---|
| 37 | // 3) enable the below code by changing to #if 1 and run to see that x2 is loaded incorrectly.
|
|---|
| 38 |
|
|---|
| 39 | //X x1 = { 0x31415926 };
|
|---|
| 40 | //BoostSave("SerializationBug.dat", x1);
|
|---|
| 41 |
|
|---|
| 42 | X x2;
|
|---|
| 43 | BoostLoad("SerializationBug.dat", x2);
|
|---|
| 44 |
|
|---|
| 45 | #if 0
|
|---|
| 46 | volatile int x = 0;
|
|---|
| 47 | if(x)
|
|---|
| 48 | {
|
|---|
| 49 | X *x3;
|
|---|
| 50 | BoostLoad("this code is never executed", x3);
|
|---|
| 51 | }
|
|---|
| 52 | #endif
|
|---|
| 53 | }
|
|---|