Perfect Forwarding and Return Values

Can perfect forwarding with std::forward be used to forward return values from a function?

Perfect forwarding with std::forward() is primarily used to forward function arguments while preserving their value category and const-ness. However, it is not typically used to forward return values from a function.

When a function returns a value, the return value is either an rvalue or an lvalue reference, depending on the return type of the function. The value category of the return value is determined by the return type and is not influenced by how the returned object is obtained within the function.

Here's an example to illustrate this:

#include <iostream>
#include <utility>

int& get_lvalue_ref() {
  static int x = 10;
  return x;
}

int get_rvalue() { return 20; }

template <typename T>
T&& forward_value(T&& value) {
  return std::forward<T>(value);
}

int main() {
  int& lvalue_ref = get_lvalue_ref();
  int rvalue = get_rvalue();

  int& forwarded_lvalue_ref =
    forward_value(lvalue_ref);
  int forwarded_rvalue =
    forward_value(rvalue);

  std::cout << "lvalue_ref: "
    << lvalue_ref << "\n";
  std::cout << "rvalue: "
    << rvalue << "\n";
  std::cout << "forwarded_lvalue_ref: "
    << forwarded_lvalue_ref << "\n";
  std::cout << "forwarded_rvalue: "
    << forwarded_rvalue << "\n";
}
lvalue_ref: 10
rvalue: 20
forwarded_lvalue_ref: 10
forwarded_rvalue: 20

In this example, get_lvalue_ref() returns an lvalue reference to a static variable x, while get_rvalue() returns an rvalue.

The forward_value() function template takes a forwarding reference value and uses std::forward<T>(value) to forward the value. However, the return type of forward_value() is T&&, which is a forwarding reference.

When forward_value() is called with lvalue_ref(), which is an lvalue reference, the returned value is still an lvalue reference. Similarly, when forward_value() is called with rvalue, which is an rvalue, the returned value is still an rvalue.

The output of the program demonstrates that the value category of the return value is determined by the return type of the function (get_lvalue_ref() and get_rvalue()) and is not affected by the use of std::forward() in forward_value().

In general, perfect forwarding with std::forward() is not necessary or beneficial for forwarding return values. The value category of the return value is already determined by the return type of the function, and using std::forward() does not provide any additional advantages in this context.

Perfect Forwarding and std::forward

An introduction to problems that can arise when our functions forward their parameters to other functions, and how we can solve those problems with std::forward

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

Perfect Forwarding with Const References
How can I perfectly forward a const lvalue reference using std::forward?
Perfect Forwarding Multiple Arguments
How can I perfectly forward multiple arguments of different types using std::forward?
Perfect Forwarding and Overload Resolution
How does perfect forwarding affect overload resolution when forwarding arguments to a function with multiple overloads?
Perfect Forwarding and Template Deduction
How does template argument deduction work when using perfect forwarding with std::forward?
Perfect Forwarding and Move-Only Types
How does perfect forwarding with std::forward handle move-only types?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant