Explain perfect forwarding and why std::forward is necessary in template wrappers.

Last updated 21 June 2026

  • Perfect forwarding lets a template wrapper pass arguments on while preserving their value category (lvalue vs rvalue) and constness. It uses forwarding references (T&& in a deduced template) where reference collapsing deduces T as U& for lvalues.

  • A named parameter is always an lvalue, so passing it naively always copies. std::forward<T>(x) casts back to an rvalue only if the original argument was an rvalue (a conditional move).

  • std::move is wrong here because it unconditionally casts to rvalue, stealing from lvalues the caller still owns.

  • https://www.geeksforgeeks.org/cpp/std-forward-in-cpp/
  • https://www.geeksforgeeks.org/interview-prep/advanced-topics-design-patterns-interview-questions-c-programming/
  • https://coderpad.io/interview-questions/cpp-interview-questions/
  • https://stackoverflow.com/questions/42790645/perfect-forwarding-and-stdmove-behavior-within-this-context
All C++ interview questions