std-function

Default function that just returns the passed value?

好久不见. 提交于 2019-11-29 12:02:54
问题 As a lazy developer, I like to use this trick to specify a default function: template <class Type, unsigned int Size, class Function = std::less<Type> > void arrange(std::array<Type, Size> &x, Function&& f = Function()) { std::sort(std::begin(x), std::end(x), f); } But I have a problem in a very particular case, which is the following: template <class Type, unsigned int Size, class Function = /*SOMETHING 1*/> void index(std::array<Type, Size> &x, Function&& f = /*SOMETHING 2*/) { for

std::function instead of templates for predicates

我们两清 提交于 2019-11-29 09:46:09
Many standard library algorithms take predicate functions. However, the type of these predicates is an arbitrary, user-provided template parameter. Why doesn't C++11 specify that these take a specific type, like std::function instead? For example: template< class InputIt > InputIt find_if( InputIt first, InputIt last, std::function<bool()> p ); Isn't using this instead of a template as argument type not much more clean? std::function is for runtime polymorphism. Any particular std::function instance could be storing a functor of any type (a type that's appropriate for the std::function 's

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

不打扰是莪最后的温柔 提交于 2019-11-29 09:31:45
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, double d, void* app_obj) { static_cast<App*>(app_obj)->callme(i,c,d); } int main() { App a; Resource r; r

std::function template argument resolution

戏子无情 提交于 2019-11-29 04:19:04
I am currently working on a library where I am chaining function objects. I am creating a function template that takes a callable object (std::function at the moment) and is parametrized on the output and input type of the function. Here is a simplified version of what I am defining: template <typename In, typename Out> std::vector<Out> process(std::vector<In> vals, std::function< Out(In) > func) { // apply func for each value in vals return result; } The problem I am having is on usage. It seems that when I pass a lambda, the compiler cannot deduce the type correctly, so complains that the

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

元气小坏坏 提交于 2019-11-29 02:11:25
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: virtual std::vector<std::shared_ptr<some::ns::TheThing>> getAllTheThings() const = 0; and successfully

Using std::function and std::bind to store callback and handle object deletion.

时光怂恿深爱的人放手 提交于 2019-11-29 01:36:22
问题 I want to implement a manager that stores callbacks to member functions of polymorphic classes using C++11. The issue is that I am not sure how to handle the case where the object that the member belongs to gets deleted or should be deleted and I want to make the interface as simple as possible. So I thought of the following: Store a std::weak_ptr to the object as well as a std::function to the member. The following seems to work: class MyBase { public: MyBase() {} virtual ~MyBase() {} }; //-

Binding to a weak_ptr

谁说我不能喝 提交于 2019-11-29 01:24:04
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() { std::weak_ptr<MyClass> thisWeakPtr(shared_from_this()); return std::function<void()>(std::bind(

C++ How to Reference Templated Functions using std::bind / std::function

我与影子孤独终老i 提交于 2019-11-29 00:59:08
问题 If you have a templated class or a templated function, (or combination of the two), how do you bind that function, (preserving the template type parameter)? I was given some help about the basic syntax in a post below, to bind to functions with explicit template type parameters, but lose the ability to provide template type parameters in the process. Is it possible to get this to work so that it is still possible to provide template type parameters with future calls? Cleaned up this code a

How are C++11 lambdas represented and passed?

喜夏-厌秋 提交于 2019-11-28 21:08:15
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 without use of new , then it means they might be like value-typed struct or classes, which defaults to

Usage and Syntax of std::function

五迷三道 提交于 2019-11-28 15:52:17
问题 It is necessary to me to use std::function but I don't know what the following syntax means. std::function<void()> f_name = []() { FNAME(); }; What is the goal of using std::function ? Is it to make a pointer to a function? 回答1: std::function is a type erasure object. That means it erases the details of how some operations happen, and provides a uniform run time interface to them. For std::function , the primary 1 operations are copy/move, destruction, and 'invocation' with operator() -- the