Perfect Forwarding and std::forward

Perfect Forwarding and Return Values

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

Abstract art representing computer programming

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.

This Question is from the Lesson:

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

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

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

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 125 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved