How do i write a pointer-to-member-function with std::function?

前端 未结 5 1128
刺人心
刺人心 2020-12-07 15:58

I know how to declare int fn(double) inside of std::function (std::function). I know how to write a pointer-to-member-function (

5条回答
  •  被撕碎了的回忆
    2020-12-07 16:28

    One of the guidelines in Scott Meyer's Modern C++11 book is to avoid std::bind and always use a lambda closure instead:

    struct A{ int fn(double){ return 0; } };
    
    std::function f = [a = A{}](double x) mutable { return a.fn(x); };
    

    The mutable is necessary here, as the capture a might potentially be changed by the function call (since A::fn is non-const).

提交回复
热议问题