C++/transient and Serialization

Is there Serialization in C++? In java, there is Serialization, and the keword word "transient" is used to specify that some members of a class can not be serialized, what does the keword word "transient" do in C++?

Thanks,
lzzzz

AnswerYou could answer this yourself by just trying it!

You would find that transient is _not_ a keyword in C++. C++ has no native support for serialisation. This follows from the differences in approach to the type systems of the likes of Java and .NET and C++.

C++ tries to do as much as it can with types (and everything else) during compilation. That is it is a statically typed system. What run time (i.e. dynamic) support there is for the type system is very limited - just about enough to allow dynamic_cast to perform cross casts when multiple inheritance is in use.

Java and .NET on the other hand are built on top of their own runtime systems - the Java Virtual Machine (JVM) for Java and the Common Language Infrastructure (or CLI) for .NET, of which the Microsoft commercial implementation is called the CLR (Common Language Runtime). This allows them to support a much more dynamic type system. One of the things you can do in these systems at runtime is reflection - find out a lot about what your objects and types are made up of. You can also create types at runtime, and even emit code (well you can in .NET, I do not know about Java). These facilities are just the sort of things you might require to help in performing tasks such as serialisation of objects automatically. You find out about the internals of the object, and from there about the members of the object type. You can then create member:value pairs for each member that requires data saved. Of course then you have to have some facility to mark members that should not be saved during serialisation, hence the transient keyword.

C++ on the other hand tries to minimize the cost in space and/or time of any feature. In particular it tries to achieve a zero overhead for any feature you are not using. That is if you are not using it you do not need to pay for it.

Because C++ is predominately statically typed most information about the types you create and use is thrown away by the compiler. This comes from C++'s focus on efficiency. Most features in C++ require very little support at runtime, other than the provision of a heap (for new/delete) and the start up code to get command line parameters into main correctly. Even the requirement for main is not required for a freestanding implementation (one that has minimal library support, only 6 of the usual C++ library headers are required). Hence the code produced can be very efficient. The exceptions are exceptions (<g>) and runtime type information required for dynamic_cast and typeinfo. You will often see compiler options to turn support for these features off so if you are not using them you incur no overhead.

Remember C++ compilers generally compile to native machine code, not Java byte code or MSIL (Microsoft Intermediate Language) as Java and .NET compilers do. (OK some compile to C or assembler code and then run a C compiler or assembler to produce a native machine code object file). On the one hand this allows C++ (like C) to be used for systems and embedded applications on systems without an operating system. On the other hand it means you have to re-compile application code for each platform. One of the stated boons of Java, if I remember correctly, was that you compiled once and ran the application everywhere (so long as everywhere had an installed compatible JVM of course!).

To add serialisation to a C++ application requires work. However there are several serialization support implementations around. A good starting point would be the Boost (http://www.boost.org/) serialisation library. The site is currently down (at least I cannot access it!), but a Google search for boost serialization produces a link to www.boost.org/libs/serialization/index.html. If this is incorrect then if you click the documentation link under libraries in the top right of the home page and select Serialization from the list of Boost libraries it should get you there. You generally have to define a function (member or free standing) to write or read the data members of the type in question to or from some archive object.

The only other alternative I can see is to concoct some sort of enhanced runtime type information system for use with C++ programs, and then build serialisation on top of this. There would still be the problem of differentiating members to be serialised and members not to be serialised! Dr. Bjarne Stroustrup did do some work in this area, see http://lcgapp.cern.ch/project/architecture/XTI_accu.pdf. I suppose such a system could be designed to allow the addition of transient/persistent markers.

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines.
Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is).
When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD.
My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes.
I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.