Boost logo

Boost :

From: Dirk Gerrits (dirkg_at_[hidden])
Date: 2002-05-12 17:18:10


> I understand that my message turned out to be a bit confusing.
>
> My original idea used to be to submit an official proposal for a future
> extension to std::string. My first implementation was a class derived from
> std::string with the new arg() method. This design worked quite fine, but
> had indeed a glitch: basic_string destructor is not virtual, so the
> polymorphic behaviour of the class was undefined.

How about this?

template<class charT, class traits = char_traits<charT> >
class mystring : private std::basic_string<charT, traits>
{
    typedef std::basic_string<charT, traits> base_type;
public:
    using base_type::begin;
    using base_type::end;
    using base_type::rbegin;
    using base_type::rend;
    using base_type::size;
    // etc etc etc

    operator const base_type& (); // implicit conversion
}

[Sorry if I got anything wrong but it's getting late here. ;)]

> I lately realized that there is no real need for a mystring class type,
> since its normal use is via unnamed temporaries (see the examples). So, my
> current idea would be to remove the mystring type, and add a format()
> function instead:
>
> std::string a = format("%1").arg(100);
>
> where format returns an implementation-defined type that can be
implicitally
> converted to std::string, and supports arg(). This way we wouldn't have to
> take the hazard of deriving from std::string, where I foresee troubles
> everywhere with dozens of different (and maybe also half-broken) standard
> libraries, and we are not losing (almost) anything syntax-wise.

This would do the formatting just fine indeed.

But the additional benifit of a class like mystring is the templated
operator+=
(and append, etc) which std::string simply doesn't provide. Consider the
following
code fragments to append an arbitrary object to a string:

std::string someString = "blablabla";
someString = (std::stringstream(someString) << someObject).str();

//OR:

std::string someString = "blablabla";
someString = format(someString + "%1").arg(someObject);

//OR:

boost::string someString = "blablabla";
someString += someObject;

Don't get me wrong though, I like the format() idea. But I think that a
custom
string class could add just a bit more functionality.

Dirk Gerrits


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk