jeudi 29 janvier 2015

Why is this move allowed?

A vector when it does a resize will attempt to use move semantics to move the objects from the old array to the new one. But if the templated object in the vector does not support a no throw noexcept move constructor then it will revert to using copy construction so that the strong exception guarantee is preserved.


But when I try this:



#include <vector>

class X
{
public:
// Needs default constructor
X() {}

// Copy operations disabled.
X(X const&) = delete;
X& operator=(X const&) = delete;

X(X&&) // throwable move constructor
{}
X& operator=(X&&) // throwable move assignment.
{return *this;}
};

int main()
{
// Vector of Size zero
std::vector<X> data;

// Vector of Size ten.
// Since the move constructor can potentially throw
// We have to copy elements when we do a resize
//
// But X has a disabled copy semantics
// Thus I would expect a compile time error here.
data.resize(10);
}


This compiles without error or warning:



> g++ --version
Configured with: --prefix=/Applications/http://ift.tt/1d5DwEL --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
> g++ -std=c++11 test.cpp
>

Aucun commentaire:

Enregistrer un commentaire