c++ - Why is basic_string::swap not noexcept? -
i found out 2 swap functions basic_string (member function , function in namespace std) not declared noexcept - neither in standard library of gcc-4.8 nor in recent c++ draft n3690.
on other hand, move constructor move assignment operator declared noexcept. shows should possible provide noexcept swap functions.
question: what's reason not having swap functions declared noexcept?
update: problem want use template function within own swap functions, uses static_assert check swap 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, works if swap functions declared noexcept, , that's not case basic_string.
paragraph 21.4.6.8 of c++11 standard specifies:
21.4.6.8
basic_string::swap[string::swap]void swap(basic_string& s);1 postcondition: *this contains same sequence of characters in s, s contains same sequence of characters in *this.
2 throws: nothing.
3 complexity: constant time.
therefore, 1 must conclude absence of noexcept oversight.
another clue given paragraph 21.4.6.3 on assign() member function:
basic_string& assign(basic_string&& str) noexcept;effects: function replaces string controlled
*thisstring of lengthstr.size()elements copy of string controlledstr. [ note: valid implementationswap(str). — end note ]3 returns:
*this.
if swap(str) supposed valid implementation assign(), , assign() marked unconditionally noexcept, makes sense assume swap() noexcept well.
Comments
Post a Comment