Function Binding and Partial Application

Binding and std::ref / std::cref

How can std::ref and std::cref be used with std::bind?

Abstract art representing computer programming

std::ref and std::cref are utility functions provided by the <functional> header that enable passing references to objects when using std::bind. These functions are particularly useful to avoid copying objects when binding them to a function.

Here's an example demonstrating how to use std::ref with std::bind:

#include <functional>
#include <iostream>

void SetValue(int& value, int newValue) {
  value = newValue; }

int main() {
  int value = 10;

  auto SetValue10 = std::bind(
    SetValue, std::ref(value), 10);

  auto SetValue20 = std::bind(
    SetValue, std::ref(value), 20);

  SetValue10();
  std::cout << "Value after SetValue10: "
    << value << '\n';

  SetValue20();
  std::cout << "Value after SetValue20: "
    << value << '\n';
}
Value after SetValue10: 10
Value after SetValue20: 20

In this example:

  • The SetValue function takes a reference to an int and a new value, updating the int with the new value.
  • We create two bound function objects, SetValue10 and SetValue20, using std::bind. Here, std::ref(value) ensures that SetValue receives a reference to value rather than a copy.
  • When SetValue10 and SetValue20 are called, they modify the original value variable.

Similarly, std::cref can be used to bind a const reference to an object, which is useful when you want to bind a reference to a const object or a temporary object that should not be modified.

Here's an example demonstrating std::cref:

#include <functional>
#include <iostream>
#include <string>

void PrintString(const std::string& str) {
  std::cout << str << '\n'; }

int main() {
  std::string tempStr = "Hello";
  auto Print = std::bind(
    PrintString, std::cref(tempStr));

  Print();
}
Hello

In this example:

  • The PrintString function takes a const reference to a std::string and prints it.
  • We create a temporary std::string object tempStr with the value "Hello".
  • We use std::cref(tempStr) to bind a const reference to tempStr.
  • The Print functor, when called, passes this const reference to PrintString, which prints the string.

By using std::cref, we ensure that the std::string object is not copied and is passed as a const reference to the bound function.

In summary, std::ref and std::cref are valuable tools for binding references to functions with std::bind, allowing you to avoid unnecessary object copying and maintain the integrity of the original objects.

This Question is from the Lesson:

Function Binding and Partial Application

This lesson covers function binding and partial application using std::bind(), std::bind_front(), std::bind_back() and std::placeholders.

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

This Question is from the Lesson:

Function Binding and Partial Application

This lesson covers function binding and partial application using std::bind(), std::bind_front(), std::bind_back() and std::placeholders.

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