Bug 165233 - Use lambdas instead of std::bind
Summary: Use lambdas instead of std::bind
Status: NEW
Alias: None
Product: LibreOffice
Classification: Unclassified
Component: LibreOffice (show other bugs)
Version:
(earliest affected)
unspecified
Hardware: All All
: medium normal
Assignee: Not Assigned
URL:
Whiteboard:
Keywords: difficultyBeginner, easyHack, skillCpp, topicCleanup
Depends on:
Blocks: Dev-related
  Show dependency treegraph
 
Reported: 2025-02-13 14:22 UTC by Hossein
Modified: 2025-02-13 14:43 UTC (History)
1 user (show)

See Also:
Crash report or crash signature:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Hossein 2025-02-13 14:22:05 UTC
Description:
std::bind is a mechanism in C++ to bind specific arguments to an existing function or function object. It is helpful to define a new function that gets fewer/different parameters but does the same thing. The parameters are forwarded to the actual underlying function alongside some other values.
https://en.cppreference.com/w/cpp/utility/functional/bind

Although std::bind is handy in some cases, it presents various drawbacks, including:

1. Reduced readability: std::bind reduces readability, as it uses syntax like std::placeholders::_1 without a meaningful name.
2. Runtime performance overhead: std::bind introduces runtime overhead.
3. Harder debugging: due to the specific syntax, it is harder to debug and find problems when dealing with function calls.
4. Issues with overloading: using std::bind, one needs to do explicit casting, which complicates using overloaded functions.

One advice is to use lambdas instead of std::bind. There are many places in LibreOffice code where std::bind is used, and the task here is to change those instances to lambda.

$ git grep std::bind | wc -l
81

You can pick an instances of using std::bind, and use lambdas to do the exact same thing. You have to make sure that the code behavior remains the same after your change.

Example:
This is an example code, which shows how to define lambda where std::bind was used before:

Use lambda instead of std::bind + wrapper function 81e9703cf8f891bfb36f52eb24e0e18ca13d373a

More information:
For understand std::bind and lambdas better, and to see some examples, please refer to the relevant C++ documentation. These are some useful links:

std::bind
https://en.cppreference.com/w/cpp/utility/functional/bind

Lambda expression in C++
https://www.geeksforgeeks.org/lambda-expression-in-c/

Lambda Vs Binders in C++ STL
https://www.geeksforgeeks.org/lambda-vs-binders-in-cpp-stl/