ProgrammingVery hardMicrosoft/Google SDE MCQ · GATE CS

What is printed?

What is printed?
void g(int&)  { cout << "L"; }
void g(int&&) { cout << "R"; }
template <class T> void fwd(T&& x) {
    g(x);
    g(std::forward<T>(x));
}
int main() {
    int i = 0;
    fwd(i);
    fwd(1);
}
  1. A. LLRR
  2. B. LRLR
  3. C. LLLL
  4. D. LLLR

Correct answer: D. LLLR

Inside fwd, the named parameter x is always an lvalue, so g(x) prints L both times. std::forward restores the category: for fwd(i), T = int& (lvalue, L); for fwd(1), T = int (rvalue, R).

More OOP with C++ and Java questions

Exams that ask this topic