Boost logo

Boost :

From: Reece Dunn (msclrhd_at_[hidden])
Date: 2004-06-21 15:56:05


Rob Stewart wrote:
>From: "Reece Dunn"
> > John Nagle wrote:
> > >Reece Dunn wrote:
> > >>John Nagle wrote:
> > >>The problems arise in the interation with basic_string_impl and how to
> > >>implement swap and functions that require constructing a string
>object.
> > >Why do you need to construct a string object to swap?
> > >Just swap one character at a time.
> >
> > [2] How do I implement the swap function? The solution I arrived at is
>the
> > one you suggest: to swap the data character-by-character. What you have
>to
>
>Unfortunately, that makes swap() O(N), but it does allow it to be
>nothrow.

Unfortunately. To swap the impl members would make it O(1), but would lead
to unpredictable behaviour since the two strings swap pointers to their
fixed_string buffers. The approach I use is the best I could find.

> > consider when doing this is that the strings don't exceed capacity,
> > resulting in additional logic.
>
>That should fail, shouldn't it? Yes, that would mean a swap()
>that throws, but the strings aren't really swapped if you only
>swap some characters.

The same is true though of all the other functions (e.g. assign): what
happens if the string doesn't fit?

>I'm not sure which is worse: a swap() that throws or only
>partially swapping. The former violates exception safety
>guarantee assumptions made by other code, but the latter means
>that swap() may not really swap, which violates semantics.

I have gone for a clip to capacity design for all the fixed_string
functions. Thus:

   fixed_string< 5 > str;
   str.assign( "Hello there!" ); // str == "Hello"

Any error handling policy that is implemented has it's own advantages and
disadvantages. Another problem with using exceptions is adapting C code:
there may not be any try-catch blocks in the code, resulting in unhandled
exceptions.

The clip strategy is not ideal either, but I believe it is the best approach
considering the code it is abstracting: you will usually have a buffer large
enough for the application you are using it for (filenames/paths, command
line strings, URLs, etc.)

One possibility is making the error handling a policy class so it can be
adapted to suit the users needs with the current behaviour as default.

Regards,
Reece

_________________________________________________________________
It's fast, it's easy and it's free. Get MSN Messenger today!
http://www.msn.co.uk/messenger


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