std-function

How to use SWIG to wrap std::function objects?

倖福魔咒の 提交于 2019-11-27 16:34:09
问题 I have seen quite a few similar questions, but have not found a solution to my particular problem. I am attempting to SWIGify some C++11 code that uses std::function, so I can use it in my Java application. I have encountered shared pointers like this: virtual std::shared_ptr<some::ns::TheThing> getTheThing(unsigned short thingID); and successfully handled them with the shared_ptr directive like so: %shared_ptr(some::ns::TheThing); I have encountered vectors of shared pointers like this:

Is it illegal to invoke a std::function<void(Args…)> under the standard?

末鹿安然 提交于 2019-11-27 13:30:48
All quotes are from N3797 . 4/3 [conv] An expression e can be implicitly converted to a type T if and only if the declaration T t=e; is well-formed, for some invented temporary variable t This implies no expression can be implicitly converted to void , as void t=e is illegal for all expressions e . This is even true if e is an expression of type void , such as void(3) . So an expression of type void cannot be implicitly converted to void . Which leads us to: 20.9.2/2 Requirements [func.require] Define INVOKE (f, t1, t2, ..., tN, R) as INVOKE (f, t1, t2, ..., tN) implicitly converted to R . In

How are C++11 lambdas represented and passed?

ε祈祈猫儿з 提交于 2019-11-27 13:30:36
问题 Passing a lambda is really easy in c++11: func( []( int arg ) { // code } ) ; But I'm wondering, what is the cost of passing a lambda to a function like this? What if func passes the lambda to other functions? void func( function< void (int arg) > f ) { doSomethingElse( f ) ; } Is the passing of the lambda expensive? Since a function object can be assigned 0, function< void (int arg) > f = 0 ; // 0 means "not init" it leads me to think that function objects kind of act like pointers. But

std::function and std::bind: what are they, and when should they be used?

做~自己de王妃 提交于 2019-11-27 09:58:40
I know what functors are and when to use them with std algorithms, but I haven't understood what Stroustrup says about them in the C++11 FAQ . Can anyone explain what std::bind and std::function are, when they should be used, and give some examples for newbies? std::bind is for partial function application . That is, suppose you have a function object f which takes 3 arguments: f(a,b,c); You want a new function object which only takes two arguments, defined as: g(a,b) := f(a, 4, b); g is a "partial application" of the function f : the middle argument has already been specified, and there are

std::function fails to distinguish overloaded functions

左心房为你撑大大i 提交于 2019-11-27 08:49:28
I am trying to understand why std::function is not able to distinguish between overloaded functions. #include <functional> void add(int,int){} class A {}; void add (A, A){} int main(){ std::function <void(int, int)> func = add; } In the code shown above, function<void(int, int)> can match only one of these functions and yet it fails. Why is this so? I know I can work around this by using a lambda or a function pointer to the actual function and then storing the function pointer in function. But why does this fail? Isn't the context clear on which function I want to be chosen? Please help me

Performance of std::function compared to raw function pointer and void* this?

邮差的信 提交于 2019-11-27 05:56:15
问题 Library code: class Resource { public: typedef void (*func_sig)(int, char, double, void*); //Registration registerCallback(void* app_obj, func_sig func) { _app_obj = app_obj; _func = func; } //Calling when the time comes void call_app_code() { _func(231,'a',432.4234,app_obj); } //Other useful methods private: void* app_obj; func_sig _func; //Other members }; Application Code: class App { public: void callme(int, char, double); //other functions, members; }; void callHelper(int i, char c,

Should I copy an std::function or can I always take a reference to it?

戏子无情 提交于 2019-11-27 04:04:53
问题 In my C++ application (using Visual Studio 2010), I need to store an std::function, like this: class MyClass { public: typedef std::function<int(int)> MyFunction; MyClass (Myfunction &myFunction); private: MyFunction m_myFunction; // Should I use this one? MyFunction &m_myFunction; // Or should I use this one? }; As you can see, I added the function argument as a reference in the constructor. But, what is the best way to store the function in my class? Can I store the function as a reference

What is the return type of a lambda expression if an item of a vector is returned?

最后都变了- 提交于 2019-11-27 03:45:05
问题 Consider the following snippet: #include <iostream> #include <vector> #include <functional> int main() { std::vector<int>v = {0,1,2,3,4,5,6}; std::function<const int&(int)> f = [&v](int i) { return v[i];}; std::function<const int&(int)> g = [&v](int i) -> const int& { return v[i];}; std::cout << f(3) << ' ' << g(3) << std::endl; return 0; } I was expecting the same result: in f , v is passed by const reference, so v[i] should have const int& type. However, I get the result 0 3 If I do not use

C++ function types

走远了吗. 提交于 2019-11-27 03:11:15
问题 I have a problem understanding function types (they appear e.g. as the Signature template parameter of a std::function ): typedef int Signature(int); // the signature in question typedef std::function<int(int)> std_fun_1; typedef std::function<Signature> std_fun_2; static_assert(std::is_same<std_fun_1, std_fun_2>::value, "They are the same, cool."); int square(int x) { return x*x; } Signature* pf = square; // pf is a function pointer, easy Signature f; // but what the hell is this? f(42); //

Binding to a weak_ptr

旧时模样 提交于 2019-11-27 02:14:51
问题 Is there a way to std::bind to a std::weak_ptr ? I'd like to store a "weak function" callback that automatically "disconnects" when the callee is destroyed. I know how to create a std::function using a shared_ptr: std::function<void()> MyClass::GetCallback() { return std::function<void()>(std::bind(&MyClass::CallbackFunc, shared_from_this())); } However the returned std::function keeps my object alive forever. So I'd like to bind it to a weak_ptr : std::function<void()> MyClass::GetCallback()