Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perfect forwarding a value of type auto

How can we forward a function auto parameter using std::forward for the below code?

void push_value(std::vector<MyType>& vec, auto&& value)
{
    vec.emplace_back(std::forward<?>(value));
}
like image 942
Michal Avatar asked Oct 11 '25 15:10

Michal


1 Answers

You want decltype(value)(value) (without forward).

Some prefer to write std::forward<decltype(value)>(value), but it does exactly the same thing, the forward is superfluous.

like image 166
HolyBlackCat Avatar answered Oct 14 '25 06:10

HolyBlackCat