Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > std::swap and exceptions

Reply
Thread Tools

std::swap and exceptions

 
 
Davis King
Guest
Posts: n/a
 
      07-20-2003
Can I assume that std::swap will not throw when it is used to swap
std::string objects?
In the standard it says that the swap() std::string member function runs
in constant time.
I didn't see anything that said that swapping strings was guaranteed to
not throw though. But it would seem
that a constant time swap ought not to throw though. I can't really see
a way to implement one that does throw without
deliberately sabotaging it at least

So can I assume std::swap won't throw for strings?

-Davis

 
Reply With Quote
 
 
 
 
Howard Hinnant
Guest
Posts: n/a
 
      07-20-2003
In article <(E-Mail Removed)>, Davis King
<(E-Mail Removed)> wrote:

| Can I assume that std::swap will not throw when it is used to swap
| std::string objects?
| In the standard it says that the swap() std::string member function runs
| in constant time.
| I didn't see anything that said that swapping strings was guaranteed to
| not throw though. But it would seem
| that a constant time swap ought not to throw though. I can't really see
| a way to implement one that does throw without
| deliberately sabotaging it at least
|
| So can I assume std::swap won't throw for strings?

swap will not throw for strings.

21.3/2 says:

| The template class basic_string conforms to the requirements of a
| Sequence, as specified in ( lib.sequence.reqmts ).

23.1.1 - Sequences [lib.sequence.reqmts], paragraph 1 says:

| A sequence is a kind of container ...

23.1 - Container requirements , paragraph 10 says:

| Unless otherwise specified ...

| no swap() function throws an exception unless that exception is thrown
| by the copy constructor or assignment operator of the container's
| Compare object (if any; see lib.associative.reqmts ).

string has no compare object. And I have not found in section 21
anything that specifies otherwise ... except:

20.1.5 - Allocator requirements, paragraphs 4 and 5:

| -4- Implementations of containers described in this International
| Standard are permitted to assume that their Allocator template
| parameter meets the following two additional requirements beyond those
| in Table ??.
| All instances of a given allocator type are required to be
| interchangeable and always compare equal to each other.
| The typedef members pointer ,const_pointer ,size_type , and
| difference_type are required to be T* ,Tconst* ,size_t , and ptrdiff_t
| , respectively.
|
| -5- Implementors are encouraged to supply libraries that can accept
| allocators that encapsulate more general memory models and that support
| non-equal instances. In such implementations, any requirements imposed
| on allocators by containers beyond those requirements that appear in
| Table ??, and the semantics of containers and algorithms when allocator
| instances compare non-equal, are implementation-defined.

So string (basic_string<char> - defaulted allocator) may not throw
because the default allocator will always compare equal. If a user
supplies a user-defined allocator into basic_string that does not
compare equal among instances, the library may not support it, and if
it does, it may support it by having a swap that throws (though
personally that would not be my first choice).

As long as you're dealing literally with string (the typedef) then I
believe it is safe to assume swap is nothrow.

--
Howard Hinnant
Metrowerks
 
Reply With Quote
 
 
 
 
Davis King
Guest
Posts: n/a
 
      07-20-2003


Howard Hinnant wrote:

> In article <(E-Mail Removed)>, Davis King
> <(E-Mail Removed)> wrote:


<snip>

> | So can I assume std::swap won't throw for strings?


<snip>

>
> As long as you're dealing literally with string (the typedef) then I
> believe it is safe to assume swap is nothrow.
>


That's what I was hoping.
Thanks, for the explanation

-Davis

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Exceptions - How do you make it work like built-in exceptions? Lie Python 3 01-14-2008 06:45 PM
Exceptions + Performance on path without exceptions gratch06@gmail.com C++ 3 04-16-2007 08:52 PM
Checked exceptions vs unchecked exceptions Ahmed Moustafa Java 5 07-14-2004 01:46 PM
ImageList Errors - Exceptions and...need HELP Herr Lucifer ASP .Net 0 06-21-2004 10:21 AM
Custom exceptions -- inherit from exceptions.Exception? Paul Miller Python 3 11-12-2003 09:24 AM



Advertisments