Boost logo

Boost :

From: Martin Wille (mw8329_at_[hidden])
Date: 2005-07-13 11:26:42


I propose the addition of a lexical_cast wrapper to Boost.Serialization.

Motivating example:

Suppose you have several UDTs for which you already have defined
insertion/extraction operators, and you want to use Boost.Serialization
to read/write values of these UDTs, preferably in a non-intrusive way.


Boost.Serialization doesn't make use of existing insertion/extraction
operators for the UDTs.

Proposed addition:

A Serialization Wrapper (similar to those described in [1]) that performs on-the-fly conversion of an
UDT value to some serializable type, usually a std::string. Code is

The proposed wrapper allows to specify a conversion that will be used
during serialization/deserialization operations. During serialization,
a value will get converted to a different type; the converted value is
written to the archive. During deserialization, the value is converted
back to the original type. The proposed wrapper uses boost::lexical cast
for the conversion.

Usage example:

  // udt.hpp
  struct udt { };
  std::ostream &operator<<(std::ostream &, udt const &);
  std::istream &operator>>(std::istream &, udt const &);

  // someclass.hpp
  #include "udt.hpp"
  #include <boost/serialization.hpp>

  struct someclass
     template<typename Archive>
     void serialize(Archive &ar, const unsigned int version)
       ar & boost::serialization::lexical_cast<std::string>(my_udt);

     udt my_udt;

  - very easy to use
  - non-intrusive

  - not very fast

  - Would you consider this a useful addition?

  - Would the name boost::serialization::lexical_cast be acceptable?

  - Do you see a use case for using an "external" type other than
    std::string with this wrapper, e.g
      ar & boost::serialization::lexical_cast<int>(my_udt); // ?
    (The code could be simplified, otherwise)

  - Similar wrappers could be written for the C++ cast operations, too.
    Would such wrappers be useful?

  - Would it be desirable to extend this to a general
    conversion-on-the-fly framework?

  - Assuming there are some positive responses about the usefulness
    of the lexical_cast wrapper, would this addition be suitable for
    a fast-track review (it is very tiny)?
    (I'd say no if this evolves into a general conversion-on-the-fly

  - other comments?

Thanks to Robert who helped me to understand my own code better.



// (C) Copyright Martin Wille 2005. Distributed under the Boost
// Software License, Version 1.0. (See


#include <boost/lexical_cast.hpp>
#include <boost/serialization/traits.hpp>
#include <boost/serialization/split_member.hpp>

namespace boost { namespace serialization {

template <class T, class Intermediate = std::string>
struct lexical_cast_wrapper
    : traits
        <lexical_cast_wrapper<T, Intermediate>
        , track_never
    explicit lexical_cast_wrapper(T &t) : value(t) {}

    template <class Archive>
    void save(Archive &ar, unsigned int) const
        Intermediate tmp((boost::lexical_cast<Intermediate>(value)));
        ar << tmp;

    template <class Archive>
    void load(Archive &ar, unsigned int) const
        Intermediate tmp;
        ar >> tmp;
        value = boost::lexical_cast<T>(tmp);


    T &value;

template <class Intermediate, class T>
lexical_cast_wrapper<T, Intermediate>
lexical_cast(T &value)
    return lexical_cast_wrapper<T, Intermediate>(value);

template <class T>
lexical_cast(T &value)
    return lexical_cast_wrapper<T>(value);



Send instant messages to your online friends

Boost list run by bdawes at, gregod at, cpdaniel at, john at