[serialization] default constructor problem

Hi, I am currently experimenting with writing a compiler tool which generates the code necessary for using boost::serialization. While working on this I found that I am unable to serialize a variable declaration which is a reference to a class constructed with the default constructor '()'( ex. class foo var(); ). I have tried to find an explanation for this in the examples, documentation and through searching the web without success. Using pointers it works fine with the default constructor and I do not see why it should be any different for references (as references in essence are pointers). Is it possible to serialize a class constructed using the default constructor using boost::serialization, and if so how? Example: #include <fstream> // include headers that implement a archive in simple text format #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> class gps_set { friend class boost::serialization::access; public: int x; gps_set::gps_set(){ x = 2;}; virtual gps_set::~gps_set(){}; gps_set::gps_set(int i) : x(1) {}; private: template<typename Archive> void serialize(Archive& ar, const unsigned int version){ ar& x; }// End method: X::serialization }; int main() { // save the schedule std::ofstream ofs("filename"); // create class instance //Compile time error if s1() is used. No error if s1(1) is used. const gps_set s1(); // save data to archive { boost::archive::text_oarchive oa(ofs); // write class instance to archive oa << s1; // archive and stream closed when destructors are called } }; Thanks, Andreas Saebjoernsen

This has nothing to do with seriaization - its a quirk of C++ syntax. Replace const gps_set s1(); with const gps_set s1; and your program will compile and execute as you expect. The problem is that C++ interprets the formor as declaration of a function named s1 which returns a const gps_set. Trying to serialize that traps on the notorious "const check" which is just a total coincidence. The later will create a const gps_set object on the stack which can be serialized with no problem. To really understand this, you'll have to dig into a little used section of one of your C++ references. Good Luck, Robert Ramey "Andreas Sæbjørnsen" <andreas.saebjoernsen@gmail.com> wrote in message news:954f1fae0601131820sfd56f77pb31a9d114f8a343e@mail.gmail.com... Hi, I am currently experimenting with writing a compiler tool which generates the code necessary for using boost::serialization. While working on this I found that I am unable to serialize a variable declaration which is a reference to a class constructed with the default constructor '()'( ex. class foo var(); ). I have tried to find an explanation for this in the examples, documentation and through searching the web without success. Using pointers it works fine with the default constructor and I do not see why it should be any different for references (as references in essence are pointers). Is it possible to serialize a class constructed using the default constructor using boost::serialization, and if so how? Example: #include <fstream> // include headers that implement a archive in simple text format #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> class gps_set { friend class boost::serialization::access; public: int x; gps_set::gps_set(){ x = 2;}; virtual gps_set::~gps_set(){}; gps_set::gps_set(int i) : x(1) {}; private: template<typename Archive> void serialize(Archive& ar, const unsigned int version){ ar& x; }// End method: X::serialization }; int main() { // save the schedule std::ofstream ofs("filename"); // create class instance //Compile time error if s1() is used. No error if s1(1) is used. const gps_set s1(); // save data to archive { boost::archive::text_oarchive oa(ofs); // write class instance to archive oa << s1; // archive and stream closed when destructors are called } }; Thanks, Andreas Saebjoernsen ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Thank you very much! After learning this I made an webpage with graphs of the Abstract Syntax Tree demonstrating an example where a variable declaration with a constructor call is interpreted as a function declaration. http://folk.uio.no/andsebjo/c++/ Thanks Andreas On 1/14/06, Sebastian Redl <sebastian.redl@getdesigned.at> wrote:
Robert Ramey wrote:
To really understand this, you'll have to dig into a little used section of one of your C++ references.
For this purpose, I recommend Scott Meyers's Effective STL. "Item 6: Be alert for C++'s most vexing parse." covers this very problem.
Sebastian Redl _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Andreas Sæbjørnsen
-
Robert Ramey
-
Sebastian Redl