9

Why move constructor for std::vector with custom allocator does not deduce a noexcept() from allocator's behaviours?

This leads to the class that encapsulates such vector cannot form the (other) vector that can be normally moved in some <algorithm>s. Even if the underlying type meets the nessesary requirements (MoveInsertable and DefaultInsertable).

8
  • because there might be people who might throw from a move constructor Apr 22, 2013 at 7:38
  • 4
    Could you provide some code? I don't understand what you mean. The noexcept on what of the "allocator's behaviours"? Apr 22, 2013 at 7:39
  • Say, "allocator's behaviours" means some boolean expression over noexcept(allocator::construct(...)) and noexcept(allocator::destroy(...)). Apr 22, 2013 at 7:42
  • @BЈовић but move constructor for vector without alternative allocator have noexcept at all. Apr 22, 2013 at 7:48
  • and move assignment operator have vector&operator=(vector&& __x) noexcept(_Alloc_traits::_S_nothrow_move()) form Apr 22, 2013 at 7:50

1 Answer 1

4

I assume that by "move constructor for std::vector with custom allocator" you mean the allocator-extended move constructor i.e. this constructor:

vector(vector&& v, const allocator_type& a);

The main reason is that if v.get_allocator() != a then the constructor must allocate more memory, which could throw bad_alloc. There is no way to know at compile-time if two allocators of a given type will always compare equal or not (I have reported this as a defect, see LWG 2108).

N.B. the standard does not require this constructor or the vector(vector&&) move constructor to be noexcept.

2
  • In this case, "the standard does not require" could be restated as "the standard does not allow", correct?
    – mcmcc
    Dec 3, 2013 at 22:50
  • @mcmcc, no, that's not correct. Implementations are allowed to add or tighten exceptions specifications, so it is conforming to add noexcept(true) for the specializations with an allocator type that is known to always compares equal (e.g. std::allocator) Dec 4, 2013 at 10:40

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.