当前位置:网站首页>Clause 34: lambda is preferred over std:: bind
Clause 34: lambda is preferred over std:: bind
2022-06-13 04:56:00 【CCSUZB】
call std::bind The returned function object is the binding object . Consider that somewhere in the program we want to set the alarm to go off after an hour and continue 30 second , Consider the following code :
using Time = std::chrono::steady_clock::time_point;
enum class Sound {
Beep, Siren, Whistle};
using Duration = std::chrono::steady_clock::duration;
// At the moment t, Make a sound s, Constantly d
void setAlarm(Time t, Sound s, Duration d);
// Accept a specified sound , The sound will be 1 Hours later , And continue 30 second
auto setSoundL =
[] (Sound s)
{
using namespace std::chrono;
setAlarm(steady_clock::now() + hours(1), s, seconds(30));
;
Then we write the corresponding std::bind Version of
auto setSoundB =
std::bind(setAlarm,
steady_clock::now() + 1h, // error ! See below
_1,
30s);
steady_clock::now() + 1h Passed as an argument to std::bind, Instead of setAlarm. It means that the expression is calling when evaluating std::bind moment . The final result is , The start time of the alarm setting is to call std::bind An hour after the moment of , Instead of setAlarm An hour when . The solution is in the original std::bind Nested inside 2 layer std::bind call :
auto setSoundB =
std::bind(setAlarm,
std::bind(std::plus<>(), steady_clock::now(), 1h),
_1,
30s);
Once you are right setAlarm Overloading is implemented , There will be new problems :
enum class Volum {
Normal, Loud, LoudPlusPlus };
void setAlarm(Time t, Sound s, Duration d, Volume v);
Previous lambda The formula will work as usual , But for std::bind Call to , Unable to compile
auto setSoundB =
std::bind(setAlarm,
std::bind(std::plus<>(), steady_clock::now(), 1h),
_1,
30s);
Because the compiler cannot determine which setAlarm Version passed to std::bind. All the information it gets is a function name , Only the function name itself is ambiguous . The solution is setAlarm Must be cast to the appropriate function pointer type :
using SetAlarm3ParamType = void(*)(Time t,Sound s, Duration d);
auto setSoundB =
std::bind(static_cast<SetAlarm3ParamType>(setAlarm),
std::bind(std::plus<>(), steady_clock::now(), 1h),
_1,
30s);
stay C++11 in std::bind There are only two limited occasions where there is a reason to use :
- Mobile capture
- Polymorphic function object
边栏推荐
- On switch() case statement in C language
- Implementing the driver registration initcall mechanism on stm32
- Several methods of identifying equivalent circuit of circuit drawing
- Solution to sudden font change in word document editing
- What is the difference between ROM, ram and flash? SRAM、DRAM、PROM、EPROM、EEPROM
- Cesium:CesiumLab制作影像切片与切片加载
- QT using layout manager is invalid or abnormal
- Simple-SR:Best-Buddy GANs for Highly Detailed Image Super-Resolution论文浅析
- Collection of some compatibility issues
- Chinese trumpet creeper
猜你喜欢

Reductive elimination

Configuration used by automatic teaching evaluation script

Cesium:cesiumlab makes image slices and loads slices

Keil uses j-link to burn the code, and an error occurs: Flash download failed - one of the "Cortex-M3" solutions

Mysql8.0.13 installation tutorial (with pictures)

Draw a hammer
![C # get all callable methods of WebService interface [webmethod]](/img/44/4429b78c5b8341ed9a4a08d75a683e.png)
C # get all callable methods of WebService interface [webmethod]

How to lay copper in AD (aluminum designer)

Hidden implementation and decoupling, knowing Pimpl mode

Section 7 - structures
随机推荐
Third party comment plugin
2022 oxidation process operation certificate examination question bank and simulation examination
Shell variable learning notes
The differences between the four startup modes of activity and the applicable scenarios and the setting methods of the two startup modes
General communication protocol for industrial Internet
Handwritten promise and its method, with detailed notes
Conception d'un système basé sur MVC avec javaswing JDBC
Stepping on a horse (one stroke)
SQL notes
Collection of some compatibility issues
Crawler scrapy framework learning 2
Must know must know -c language keywords
Go/golang connection to database
How to understand JS expressions and JS statements
C language exercise 1
Advanced C language - Section 1 - data storage
[JS solution] leedcode 200 Number of islands
Win8.1和Win10各自的优势
Force buckle 92 Reverse linked list II
Clause 26: avoid overloading universal reference types