Boost logo

Boost :

From: Ulrich Eckhardt (doomster_at_[hidden])
Date: 2007-12-04 06:12:49

On Tuesday 04 December 2007 09:29:45 Corrado Zoccolo wrote:
> You can improve things by swapping the string instead of using assignment.
> template<class Archive, class Elem, class Tr>
> -basic_binary_iprimitive<Archive, Elem, Tr>::load(std::string & s)
> +basic_binary_iprimitive<Archive, Elem, Tr>::load(std::string & res_s)
> {
> + std::string s;
> std::size_t l;
> this->This()->load(l);
> // borland de-allocator fixup
> @@ -96,6 +97,9 @@
> s.resize(l);
> // note breaking a rule here - could be a problem on some platform
> load_binary(const_cast<char *>(, l);
> +
> + // Respect other references to the chars by assigning result string
> *+ res_s.swap(s);*
> }

How about this:

  std::size_t l;

  std::string tmp(l, '\0');
  /* We write directly into the string's buffer using a const_cast

  Note: this relies on the implementation to provide an own buffer
  to every std::string, which we try to assure by using a temporary
  here. We are _not_ using resize() on the existing string because
  if the size remains the same this is a no-op and does not cause a
  buffer to be allocated on e.g. GCC4's libstdc++. */
  load_binary(const_cast<char*>(, tmp.size());


This might even make the Borland workaround unnecessary. Note also that I used and tmp.size() in the load_binary call just for symmetry.


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