Opened 7 years ago

Last modified 7 years ago

#11927 new Bugs

optional_test_swap.cpp: In C++11 mode, std::swap should have a noexcept specification

Reported by: Aparna Kumta <aparna.kumta@…> Owned by: Fernando Cacciola
Milestone: To Be Determined Component: optional
Version: Boost Development Trunk Severity: Problem
Keywords: Cc:

Description

Compiling libs/optional/test/optional_test_swap.cpp with Oracle Solaris Studio 12.5 in C++11 mode, we see the following error: "../libs/optional/test/optional_test_swap.cpp", line 239: Error: The prior declaration for std::swap<optional_swap_test::class_whose_default_ctor_should_not_be_used>(optional_swap_test::class_whose_default_ctor_should_not_be_used&, optional_swap_test::class_whose_default_ctor_should_not_be_used&) has an exception specification. "../libs/optional/test/optional_test_swap.cpp", line 245: Error: The prior declaration for std::swap<optional_swap_test::class_without_default_ctor>(optional_swap_test::class_without_default_ctor&, optional_swap_test::class_without_default_ctor&) has an exception specification. 2 Error(s) detected.

See: http://www.boost.org/development/tests/develop/developer/output/oracle-intel-S2-12-5-cpp11-boost-bin-v2-libs-optional-test-optional_test_swap-test-sun-12-5_cpp11-release-threading-multi.html

In C++03, the std::swap template does not have an exception specification. In C++11, it has a noexcept specification.

This seems to be a violation of the standard. 15.4[except.spec]p5: ===== If any declaration of a function has an exception-specification that is not a noexcept-specification allowing all exceptions, all declarations, including the definition and any explicit specialization, of that function shall have a compatible exception-specification.

Change History (2)

comment:1 by Aparna Kumta <aparna.kumta@…>, 7 years ago

comment:2 by anonymous, 7 years ago

In C++11 std::swap has the following noexcept specification (20.2.2 [utility.swap]):

noexcept(is_nothrow_move_constructible<T>::value &&
         is_nothrow_move_assignable<T>::value

Because optional_swap_test::class_without_default_ctor has a throwing move assignment, therefore the non-specialized instantiation of std::swap for class_without_default_ctor is noexcept(false). The explicit specialization in file optional_test_swap.cpp is correct not to provide noexcept specification.

My guess is that the compiler gets the is_nothrow_move_assignable wrong.

Note: See TracTickets for help on using tickets.