I always read that std::forward is only for use with template parameters. However, I was asking myself why. See the following example:
void Imag
I recommend reading "Effective Modern C ++" by Scott Meyers, specifically:
std::move and std::forward.From a purely technical perspective, the answer is yes:
std::forwardcan do it all.std::moveisn’t necessary. Of course, neither function is really necessary, because we could write casts everywhere, but I hope we agree that that would be, well, yucky.std::move’s attractions are convenience, reduced likelihood of error, and greater clarity.
This function accepts rvalues and cannot accept lvalues.
void ImageView::setImage(Image&& image){
_image = std::forward(image); // error
_image = std::move(image); // conventional
_image = std::forward(image); // unconventional
}
Note first that std::move requires only a function argument, while std::forward requires both a function argument and a template type argument.
This function accepts all and does perfect forwarding.
template void ImageView::setImage(T&& image){
_image = std::forward(image);
}