473,388 Members | 1,340 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

std::swap and exceptions

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

Jul 19 '05 #1
2 7806
In article <3F***************@mail.com>, Davis King
<da*******@mail.com> 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
Jul 19 '05 #2


Howard Hinnant wrote:
In article <3F***************@mail.com>, Davis King
<da*******@mail.com> 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

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Steve Hill | last post by:
Hi, suppose I have a vector allocated on the heap. Can I use a temporary (stack based vector) and swap to clear it, or is this unsafe. e.g. vector<int> *v; vector<int> tmp; v->swap(tmp); // is...
17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Jason Heyes | last post by:
Does the STL have a function like this one? template <typename T> void remove(std::vector<T> &v, std::vector<T>::size_type index) { std::swap(v, v.back()); v.resize(index); } Unlike...
1
by: Sung Jin Hwang | last post by:
Hi. Is there a good way to extend std::swap to user type classes? Overloading can be a solution but when user type is not a class but class template, we need partial specialization and function...
4
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
1
by: ma740988 | last post by:
Consider the source snippet. # include <iostream> struct foo_struct { int odx ; int pdx ; foo_struct () : odx ( 0 ) , pdx ( 0 )
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.