std-function

Functor reference through a std::function

旧时模样 提交于 2019-12-06 16:54:19
问题 Basically, I would like to have the following semantic : #include <functional> #include <iostream> class test { public: void add(std::function<void()> f) { f(); } void operator()() { ++x; } int x = 33; }; int main() { test t; t.add(t); // wanted: x == 34 instead: x == 33 since add(t) copies it } I understand std::function wraps a copy of the callable object but is there any way to get a reference to a callable object using std::function? 回答1: You want to use the std::ref template function to

std::list containing std::function

南楼画角 提交于 2019-12-06 13:16:10
What I'm trying to achieve is std::list that contains std::functions . I'm trying to implement a callback system where functions can be added to the list and then the list can be looped through and each function called. What I have in class A is: std::list<std::function<void( bool )>> m_callbacks_forward; bool registerForward( std::function<void(bool)> f ) { m_callbacks_forward.push_back ( f ); return true; }; void GameControllerHub::invokeForward( bool state ) { for( std::list<std::function<void(bool)>>::iterator f = m_callbacks_forward.begin(); f != m_callbacks_forward.end(); ++f ){ f(); } }

Replacing std::function from within itself (by move-assignment to *this?)

喜欢而已 提交于 2019-12-06 05:13:47
问题 Is it possible to replace one std::function from within itself with another std::function ? The following code does not compile: #include <iostream> #include <functional> int main() { std::function<void()> func = []() { std::cout << "a\n"; *this = std::move([]() { std::cout << "b\n"; }); }; func(); func(); func(); } Can it be modified to compile? The error message right now is: 'this' was not captured for this lambda function - which I completely understand. I don't know, however, how I could

How to make these std::function parameters unambiguous?

三世轮回 提交于 2019-12-05 20:14:02
The following function overloads are ambiguous when passing a lambda. I found out that std::function can be constructed from most callable types , even if their signature does not match. So the compiler can't tell which function to use. template <typename T> void each(std::function<void(T)> iterator); template <typename T> void each(std::function<void(T, id)> iterator); template <typename T> void each(std::function<void(T&)> iterator); template <typename T> void each(std::function<void(T&, id)> iterator); There are some similar questions out here, but none of them could solve my problem. How

assigning std::function to a member function

不打扰是莪最后的温柔 提交于 2019-12-05 18:51:56
class A { public: std::function<void(int)> f_; void print_num(int i) { cout << i; } void setFuntion(std::function<void(int)> f) { f_=f; } void run() { setFunction(print_num); } }; this doesn't work. i get note: no known conversion for argument 1 from ‘<unresolved overloaded function type>’ to ‘std::function<void(int)>’ and other errors. If I put the definition of print_num outside of the class. everything works. i tried adding &A:: , A:: and this. nothing helped. JohnB print_num is a non-static member function, which means that it has an implicit first argument of type A* . You can, for

Binding a std::function to the same function of a different object instance

Deadly 提交于 2019-12-05 16:41:24
Is it possible to rebind a std::function to point to the same function but with a different object instance? Say if I have an object that has a std::function that is bound to another function, but if that object was copied to another instance, I'd like to rebind the std::function to that new instance instead of the old instance. #include "stdafx.h" #include <iostream> #include <functional> class EventHandler { public: int Num; std::function<int()> OnEvent; EventHandler (int inNum) { Num = inNum; } EventHandler (const EventHandler& other) { Num = other.Num; OnEvent = other.OnEvent; //TODO: Need

Strange behavior with std::function

…衆ロ難τιáo~ 提交于 2019-12-05 15:12:38
I'm using the standard function wrapper from the C++11 library, and I am seeing some strange behavior with its boolean operator. If I create a std::function object the boolean operator returns false. This is still true if I assign nullptr to the object and check again. The problem appears when I assign it a void pointer which I have cast into a function pointer. Consider the following program: #include <functional> #include <iostream> void* Test() { return nullptr; } int main(int argc, char* argv[]) { std::function<void()> foo; std::cout << !!foo << std::endl; foo = nullptr; std::cout << !!foo

Initialize class containing a std::function with a lambda

元气小坏坏 提交于 2019-12-05 12:57:15
I created a template class containing a std::function as a member the following way: template<typename Ret, typename... Args> class Foo { private: std::function<Ret(Args...)> _func; public: Foo(const std::function<Ret(Args...)>& func): _func(func) {} }; In order not to have to specify the arguments and return type of the passed function, I created some make_foo overloads: template<typename Ret, typename... Args> auto make_foo(Ret (&func)(Args...)) -> Foo<Ret, Args...> { return { std::function<Ret(Args...)>(func) }; } template<typename Ret, typename... Args> auto make_foo(const std::function

How can I use polymorphism with std::function?

时光怂恿深爱的人放手 提交于 2019-12-05 11:58:57
Let's say I have 2 classes: class A {} class B : public A {} And i want to use an std::function the receives anything of type A , but with assign to it methods that receive classes that inherit from A (like B ). void myFun(B bbbb) {} std::function<void(A)> blah = std::bind(myFun, _1); This obviously doesn't work, because the compiler won't just downcast implicitly. But how can I do something like this ? Basically I want to hold a map of some basic std::function type, and in each mapped value it will hold an std::function to a derived type like B . Is there a way to bind a cast operator to the

std::function copying parameters?

帅比萌擦擦* 提交于 2019-12-05 03:28:21
My code: #include <iostream> #include <functional> using namespace std; struct A { A() = default; A(const A&) { cout << "copied A" << endl; } }; void foo(A a) {} int main(int argc, const char * argv[]) { std::function<void(A)> f = &foo; A a; f(a); return 0; } I'm seeing "copied A" twice on the console. Why is the object being copied twice, not once? How can I prevent this properly? Luc Danton The specialization std::function<R(Args...)> has a call operator with the following declaration: R operator()(Args...) const; In your case, this means that the operator takes A . As such, calling f(a)