Perfect Forwarding and std::forward

Perfect Forwarding and Overload Resolution

How does perfect forwarding affect overload resolution when forwarding arguments to a function with multiple overloads?

Abstract art representing computer programming

Perfect forwarding using std::forward() can have an impact on overload resolution when forwarding arguments to a function with multiple overloads. The forwarded arguments maintain their value category and const-ness, which can influence which overload gets selected.

Consider the following example:

#include <iostream>

void process_data(int& x) {
  std::cout << "process_data(int&): "
    << x << "\n";
}

void process_data(const int& x) {
  std::cout << "process_data(const int&): "
    << x << "\n";
}

void process_data(int&& x) {
  std::cout << "process_data(int&&): "
    << x << "\n";
}

template <typename T>
void forward_data(T&& arg) {
  process_data(std::forward<T>(arg));
}

int main() {
  int a = 10;
  const int b = 20;

  forward_data(a);
  forward_data(b);
  forward_data(30);
}
process_data(int&): 10
process_data(const int&): 20
process_data(int&&): 30

In this example, process_data() has three overloads:

  1. process_data(int& x): Takes a non-const lvalue reference.
  2. process_data(const int& x): Takes a const lvalue reference.
  3. process_data(int&& x): Takes an rvalue reference.

The forward_data() function template takes a forwarding reference arg and forwards it to process_data() using std::forward<T>(arg).

When forward_data() is called with different arguments:

  1. forward_data(a)a is an lvalue, so T deduces to int&. The overload process_data(int&) is selected.
  2. forward_data(b)b is a const lvalue, so T deduces to const int&. The overload process_data(const int&) is selected.
  3. forward_data(30)30 is an rvalue, so T deduces to int. The overload process_data(int&&) is selected.

Perfect forwarding with std::forward() ensures that the forwarded arguments maintain their value category and const-ness, allowing the appropriate overload of process_data() to be selected based on the argument type.

This demonstrates how perfect forwarding can impact overload resolution when forwarding arguments to a function with multiple overloads, enabling the selection of the most suitable overload based on the forwarded argument type.

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