This seems to be most relavant question already asked.
Whats the difference between std::move and std::forward
But each answer is different and applies and says slightly different things. So I am confused.
I have the following situation.
emplaceBackInternal()
(Please say otherwise if I am wrong).The Code:
template<typename T>
class Container
{
std::size_t length;
T* buffer;
public:
void push_back(T const& value)
{
resizeIfRequired();
pushBackInternal(value);
}
template<typename... Args>
void emplace_back(Args&&... args)
{
resizeIfRequired();
emplaceBackInternal(std::forward<T>(arg)...);
}
void push_back(T&& value)
{
resizeIfRequired();
// Is this forward correct or should it be move
moveBackInternal(std::forward<T>(value));
}
private:
void pushBackInternal(T const& value)
{
// Copy construct object into buffer;
new (buffer + length) T(value);
++length;
}
template<typename... Args)
void emplaceBackInternal(Args&&... args)
{
// Construct object into buffer using arguments;
new (buffer + length) T(std::forward<T>(args)...);
++length;
}
void moveBackInternal(T&& value)
{
// Move construct object into buffer;
// Is this forward correct or should it be move
new (buffer + length) T(std::forward<T>(value));
++length;
}
};
I include all three here to compare the three functions with the answers provided in the previously mentioned answer. The main reason is that move
and construct
looks so similar that it feels like they should be the same.
Answer @Potatoswatter Score 67
std::forward has a single use case: to cast a templated function parameter
By this definition I should be using std::move
inside push_back(T&& value)
and
moveBackInternal(T&& value)
as the value is not a template parameter for the function.
Answer @Howard Hinnant Score 38
If Y is an lvalue reference, the result will be an lvalue expression. If Y is not an lvalue reference, the result will be an rvalue (xvalue to be precise) expression.
Seems by this definition I can use either std::move
or std::forward
.
Answer @Bo Persson Score 11
std::forward is used to forward a parameter exactly the way it was passed to a function.
Seems to say that std::forward
is acceptable (though if I follow the link in the answer all the examples use templated functions).
In this case:
void push_back(T&& value)
{
resizeIfRequired();
moveBackInternal(std::forward<T>(value)); // (1)
moveBackInternal(std::move(value)); // (2)
}
std::forward<T>(value)
and std::move(value)
are identical in this scenario (it doesn't matter between (1)
and (2)
... so use (2)
).
move
is an unconditional cast to xvalue. That line gives you an expression of type T&&
that's an rvalue, always.
forward
is a conditional cast. If T
is an lvalue reference type, it yields an lvalue. Otherwise (if it's either not a reference type or an rvalue reference type), it yields an rvalue. In our case, T
is not a reference type - so we get an rvalue.
Either way, we end up at the same point - we call moveBackInternal
with value
cast as an rvalue. Just move()
is a simpler way of getting there. forward<T>
works, but it's unnecessary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With