Function Binding and Partial Application

Binding and Lambda Expressions

Can std::bind be used with lambda expressions?

Abstract art representing computer programming

Yes, std::bind can be used with lambda expressions. Lambda expressions in C++ are essentially unnamed function objects, and they can be bound using std::bind just like regular functions or function objects.

Consider the following example:

#include <functional>
#include <iostream>

int main() {
  auto Lambda = [](int a, int b) {  
    return a + b;
  };

  auto BoundLambda{
    std::bind(Lambda, std::placeholders::_1, 10)
  };  

  std::cout << BoundLambda(5) << '\n';
  std::cout << BoundLambda(20) << '\n';
}
15
30

In this example, we define a lambda expression Lambda that takes two int arguments and returns their sum.

We then use std::bind to bind Lambda and create a new functor BoundLambda. The first argument of BoundLambda is bound to the placeholder std::placeholders::_1, and the second argument is bound to the value 10.

When BoundLambda is called with an argument, it forwards that argument to the first parameter of Lambda, while the second parameter of Lambda is fixed to 10.

Binding lambda expressions can be useful in scenarios where you want to create custom function objects on the fly and bind some of their arguments.

Here's another example that demonstrates binding a lambda expression with a captured variable:

#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>

int main() {
  std::vector<int> Values{1, 2, 3, 4, 5};
  int Threshold = 3;

  auto GreaterThan = [Threshold](int Value) {  
    return Value > Threshold;
  };

  auto BoundGreaterThan = std::bind(
    GreaterThan, std::placeholders::_1);  

  auto Count{std::count_if(
    Values.begin(), Values.end(),
    BoundGreaterThan)
  };  

  std::cout << "Count: " << Count << '\n';
}
Count: 2

In this example, the lambda expression GreaterThan captures the Threshold variable by value. It checks if a given Value is greater than the captured Threshold.

We bind GreaterThan using std::bind to create BoundGreaterThan, which takes a single argument.

Finally, we use BoundGreaterThan as the predicate for std::count_if to count the number of elements in the Values vector that are greater than the Threshold.

Binding lambda expressions allows you to create custom function objects with captured state and bind their arguments as needed.

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:

  • 124 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