1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. In template void f(T&& x), what is T deduced as when the…

Added 30 Nov -0001

In template<class T> void f(T&& x), what is T deduced as when the function is called with an lvalue int?

  1. int&, because the forwarding reference collapses to an lvalue reference Correct answer
  2. int, because the reference in the parameter is stripped before deduction
  3. int&&, because the parameter is declared as an rvalue reference to T
  4. const int&, because lvalues are deduced as const in template deduction

Correct answer

A. int&, because the forwarding reference collapses to an lvalue reference

13 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs