3

I was writing some code that has a generic container that requires elements to be nothrow_move_constructible.

I decided to add a static_assert that enforces this, just in case.

To my surprise I can't compile now when using boost::container::flat_set.

I assumed that this was just an oversight and I need a more recent boost verison, but it seems that actually they deliberately made it not safely movable:

See docs here:

http://www.boost.org/doc/libs/1_61_0/doc/html/boost/container/flat_set.html

You can see that they did update it to use R-value references and marked swap as noexcept, but they chose not to make the move ctor noexcept. It appears that move assignment is conditionally noexcept. The condition appears to depend on the value type and on the allocator in some way.

What could be the rationale for not being nothrow move constructible? Is it just an oversight?

2

1 Answer 1

2

If the objects within a container are not nothrow_move_constructible then it is very dangerous to take an entire set of one container and relocate it to another under certain conditions (usually involving Allocators). If two containers were not constructed with the same allocator, then it is no longer safe to move memory from one container to another (think two containers from two different memory arenas).

Digging in to the current source, both the contract and the implementation are problematic:

//! <b>Effects</b>: Move constructs a flat_map.
//!   Constructs *this using x's resources.
flat_map(BOOST_RV_REF(flat_map) x)
    : m_flat_tree(boost::move(x.m_flat_tree))
{ ... }

So your current expectation that it could currently by nothrow is correct. But what they are doing is probably not right.

I can only guess that they are worried that they will have to do revisit this in the future and don't want to have to weaken the nothrow contract later.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.