Consider the following code, which tries to move-construct a shared_ptr
, but due to a mistake appears to copy-construct it:
#include <utility>
#include <cassert>
#include <memory>
int main()
{
const auto x=std::make_shared<int>(4325); // can't be moved from
const std::shared_ptr<int> y(std::move(x)); // silently copy-constructs
assert(x==nullptr); // fails
}
Here the fact that due to x
being const
, y
was copy-constructed instead of move-construction will only be detected at runtime. Is there any way to make sure move really occurs, at compile time?