std::swap and exceptions

Discussion in 'C++' started by Davis King, Jul 20, 2003.

  1. Davis King

    Davis King Guest

    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
    Davis King, Jul 20, 2003
    #1
    1. Advertising

  2. In article <>, Davis King
    <> 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
    Howard Hinnant, Jul 20, 2003
    #2
    1. Advertising

  3. Davis King

    Davis King Guest

    Howard Hinnant wrote:

    > In article <>, Davis King
    > <> 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
    Davis King, Jul 20, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marcin Kalicinski

    Custom swap in std namespace

    Marcin Kalicinski, Dec 5, 2005, in forum: C++
    Replies:
    2
    Views:
    332
    red floyd
    Dec 5, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    711
    Andrew Koenig
    Jan 15, 2006
  3. Niels Dekker (no reply address)

    What swap is called when using std::swap?

    Niels Dekker (no reply address), Jul 19, 2006, in forum: C++
    Replies:
    4
    Views:
    976
    Niels Dekker (no reply address)
    Jul 20, 2006
  4. ma740988
    Replies:
    9
    Views:
    784
  5. ma740988

    composite types and std::swap

    ma740988, Mar 24, 2007, in forum: C++
    Replies:
    1
    Views:
    261
    Alf P. Steinbach
    Mar 24, 2007
Loading...

Share This Page