std-function

What is the difference between std::function and std::mem_fn

Deadly 提交于 2019-12-03 12:57:37
I am having trouble figuring out the difference between the two function wrappers std::function and std::mem_fn . From the description, it seems to me that std::function does everything std::mem_fn does and more. In which instance would one use std::mem_fn over std::function ? You can't really compare std::function with std::mem_fn . The former is a class template whose type you specify, and the latter is a function template with unspecified return type. There really isn't a situation in which you'd actually consider one versus the other. A better comparison might be between mem_fn and std:

Understanding std::function and std::bind

限于喜欢 提交于 2019-12-03 12:25:57
I was playing arround with std::function and std::bind and I noticed something unintuitive and I would like to understand it better. For example: void fun() { } void hun(std::string) { } int main() { function<void(int)> g = &fun; //This fails as it should in my understanding. function<void(int)> f = std::bind(fun); //This works for reasons unknown to me function<void(int, std::string)> h = std::bind(hun); //this doesn't work return 0; } How is it possible to bind a function<void(int)> to a function that is void() . I could then call f(1) and get fun(). I would like to understand how this is

binding member functions in a variadic fashion

こ雲淡風輕ζ 提交于 2019-12-03 11:15:15
I have a member function with a variable number of parameters, stored in a std::function , and I want to bind the instance and get an independent function object. template <class T, class R, class... Args> void connect(const T& t, std::function<R(const T&, Args...)> f) { std::function<R(Args...)> = /* bind the instance c into the function? */ } // ... Class c; connect(c, &Class::foo); For a fixed number of arguments I'd use std::bind , but I don't see how to do this for variadic parameters. The solution is very simple, since you already have the amount of arguments and everything: template

C++11 std::function and perfect forwarding

限于喜欢 提交于 2019-12-03 10:48:26
Why definition of std::function<>::operator() in the C++ standard is: R operator()(ArgTypes...) const; and not R operator()(ArgTypes&&...) const; ? One would think that to correctly forward parameters, we need the && and then use std::forward<ArgTypes>... in the function body when forwarding the call? I partially reimplemented std::function to test this and I found out that if I use the &&, I get "cannot bind 'xxx' lvalue to 'xxx&&'" from g++ when I try later to pass parameters by value to operator(). I thought that I got enough grasp of the rvalue/forwarding concepts, but still I cannot grok

How to properly check if std::function is empty in C++11?

隐身守侯 提交于 2019-12-03 10:21:03
问题 I was wondering how to properly check if an std::function is empty. Consider this example: class Test { std::function<void(int a)> eventFunc; void registerEvent(std::function<void(int a)> e) { eventFunc = e; } void doSomething() { ... eventFunc(42); } }; This code compiles just fine in MSVC but if I call doSomething() without initializing the eventFunc the code obviously crashes. That's expected but I was wondering what is the value of the eventFunc ? The debugger says 'empty' . So I fixed

How to properly check if std::function is empty in C++11?

我的未来我决定 提交于 2019-12-03 00:52:32
I was wondering how to properly check if an std::function is empty. Consider this example: class Test { std::function<void(int a)> eventFunc; void registerEvent(std::function<void(int a)> e) { eventFunc = e; } void doSomething() { ... eventFunc(42); } }; This code compiles just fine in MSVC but if I call doSomething() without initializing the eventFunc the code obviously crashes. That's expected but I was wondering what is the value of the eventFunc ? The debugger says 'empty' . So I fixed that using simple if statement: void doSomething() { ... if (eventFunc) { eventFunc(42); } } This works

C++11 std::set lambda comparison function

∥☆過路亽.° 提交于 2019-12-02 15:20:33
I want to create a std::set with a custom comparison function. I could define it as a class with operator() , but I wanted to enjoy the ability to define a lambda where it is used, so I decided to define the lambda function in the initialization list of the constructor of the class which has the std::set as a member. But I can't get the type of the lambda. Before I proceed, here's an example: class Foo { private: std::set<int, /*???*/> numbers; public: Foo () : numbers ([](int x, int y) { return x < y; }) { } }; I found two solutions after searching: one, using std::function . Just have the

Why `is_constructible<function<int(int)>, int(*)(int,int)>::value` is true under VC2015RC

丶灬走出姿态 提交于 2019-12-02 04:02:41
#include <functional> using namespace std; int main() { static_assert(is_constructible<function<int(int)>, int(*)(int,int)>::value, "error"); } The code doesn't compile with GCC and Clang, but passed with Visual C++ 2015 RC. Is this standard compliant behavior or just a bug? std::function 's constructor used to accept everything under the sun (it was a template<class F> function(F f) ). Then it got constrained in the standard (by LWG issue 2132 ), but implementing that constraint requires expression SFINAE, which Microsoft's compiler doesn't yet support . 来源: https://stackoverflow.com

Convert lambda with capture clause stored in std::function to raw function pointer

喜你入骨 提交于 2019-12-01 16:41:43
Since my last recent question was unfortunately worded and resulted in a solution to another problem then mine, here I will try to formulate my actual problem in a clear way. Before we start, as a sidenote, I am integrating the Javascript Engine V8 into my C++ application. That's where all the types in the example come from. And that's also the reason for the fact that I need a raw function pointer in the end. But I elaborate on this below. From inside a class I need to pass a lambda expression with the capture clause [=] as parameter of the type std::function to another function and cast it

Convert lambda with capture clause stored in std::function to raw function pointer

 ̄綄美尐妖づ 提交于 2019-12-01 15:32:54
问题 Since my last recent question was unfortunately worded and resulted in a solution to another problem then mine, here I will try to formulate my actual problem in a clear way. Before we start, as a sidenote, I am integrating the Javascript Engine V8 into my C++ application. That's where all the types in the example come from. And that's also the reason for the fact that I need a raw function pointer in the end. But I elaborate on this below. From inside a class I need to pass a lambda