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
> > >Why do you need to construct a string object to swap?
> > >Just swap one character at a time.
> >  How do I implement the swap function? The solution I arrived at is
> > one you suggest: to swap the data character-by-character. What you have
>Unfortunately, that makes swap() O(N), but it does allow it to be
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
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
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.
It's fast, it's easy and it's free. Get MSN Messenger today!