I just noticing a std::vector<Foo>
of mine was copying instead of moving its elements when resizing - even though Foo
has a move ctor:
class Foo {
// ...
Foo(Foo&& other) : id_(other.id_), ptr_(other.ptr_), flag(other.flag)
{
other.flag = false;
};
// ...
int id_;
void* ptr_;
bool flag;
}
Then I read:
Resize on std::vector does not call move constructor
which reminded me that std::vector
will only use move construction if the elements' move ctor is declared noexcept
. When I add noexcept
, move ctor's are called.
My question is: Why, given the move ctor's code, does the compiler not determine it to be noexcept
? I mean, it can know for a fact that an exception cannot be thrown. Also, is inferring noexcept
disallowed by the standard, or not just done by my specific compiler?
I'm using GCC 5.4.0 on GNU/Linux.