Stop r value causing C ++ 11 move assignment

Consider a simple C ++ class that I don't want to change:

class foo {};

Then, if I do the following, I will call the move assignment operator:

foo f{};

f = foo{};

Is there a way to invoke the assignment of a copy without modification fooor using an intermediate of the gfollowing form:

foo f{};
foo g{};
f = g;

Almost as if it were std::dont_move!

+4
source share
2 answers

std::dont_move() easy to implement on your own:

template <typename T>
const T& force_copy(T&& v)
{
    return v;
}

See usage example

+6
source

You can write:

f = static_cast<foo const&>(foo{});
+4
source

Source: https://habr.com/ru/post/1669354/


All Articles