Search code examples
c++functionparametersautoforward

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));
}

Solution

  • 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.