Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is basic_string::swap not noexcept?

Tags:

I just found out that the two swap functions for basic_string (member function and function in namespace std) are not declared with noexcept - neither in the standard library of GCC-4.8 nor in the most recent C++ draft N3690.

On the other hand, the move constructor as well as the move assignment operator are declared with noexcept. That shows that is should be possible to provide noexcept swap functions.

Question: What's the reason for not having the swap functions declared with noexcept?

Update: The problem is that I want to use a template function within my own swap functions, that uses static_assert to check that the swap is actually noexcept, e.g.:

struct foo {
    bar_t bar;
    baz_t baz;
    void swap(foo& rhs) noexcept {
        swap_noexcept(bar, rhs.bar);
        swap_noexcept(baz, rhs.baz);
    }
};

However, that works only if the swap functions are declared with noexcept, and that's not the case for basic_string.

like image 887
nosid Avatar asked May 18 '13 22:05

nosid


Video Answer


1 Answers

Paragraph 21.4.6.8 of the C++11 Standard specifies:

21.4.6.8 basic_string::swap [string::swap]

void swap(basic_string& s);

1 Postcondition: *this contains the same sequence of characters that was in s, s contains the same sequence of characters that was in *this.

2 Throws: Nothing.

3 Complexity: constant time.

Therefore, one must conclude the absence of noexcept is an oversight.

Another clue is given by paragraph 21.4.6.3 on the assign() member function:

basic_string& assign(basic_string&& str) noexcept;

Effects: The function replaces the string controlled by *this with a string of length str.size() whose elements are a copy of the string controlled by str. [ Note: A valid implementation is swap(str). — end note ]

3 Returns: *this.

If swap(str) is supposed to be a valid implementation for assign(), and assign() is marked as unconditionally noexcept, then it makes sense to assume swap() to be noexcept as well.

like image 198
Andy Prowl Avatar answered Sep 19 '22 12:09

Andy Prowl