Binding and Lambda Expressions

Can std::bind be used with lambda expressions?

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.

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.

Questions & Answers

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

Binding Member Variables
Can std::bind be used to bind member variables in addition to member functions?
Binding and Const Member Functions
How does std::bind handle const member functions?
Binding and Function Templates
Can std::bind be used with function templates?
Binding and Function Composition
How can std::bind be used for function composition?
Binding and std::ref / std::cref
How can std::ref and std::cref be used with std::bind?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant