In template<class T> void f(T&& x), what is T deduced as when the function is called with an lvalue int?
Correct answer
A. int&, because the forwarding reference collapses to an lvalue reference
13 views
A. int&, because the forwarding reference collapses to an lvalue reference
13 views
A parameter written T&& where T is a deduced template parameter is a forwarding reference: an lvalue argument deduces T as int&, and reference collapsing turns int& && into int&. An rvalue argument deduces T as plain int, making the parameter int&&. Passing std::forward<T>(x) onward restores the original value category, which is the entire point of the idiom.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.