std-function

Can std::async call std::function objects?

不打扰是莪最后的温柔 提交于 2020-02-21 11:54:45
问题 Is it possible to call function objects created with std::bind using std::async. The following code fails to compile: #include <iostream> #include <future> #include <functional> using namespace std; class Adder { public: int add(int x, int y) { return x + y; } }; int main(int argc, const char * argv[]) { Adder a; function<int(int, int)> sumFunc = bind(&Adder::add, &a, 1, 2); auto future = async(launch::async, sumFunc); // ERROR HERE cout << future.get(); return 0; } The error is: No matching

How do I store a vector of std::bind without a specific case for the template?

寵の児 提交于 2020-02-01 04:02:51
问题 After going though a question on std::bind , I was wondering if it was possible to hold a vector of functions created by std::bind so I can avoid using std::function and its heavyweight wrapping. #include <iostream> #include <functional> #include <typeinfo> #include <vector> int add(int a, int b) {return a + b;} int main() { //I believe this here is just a special type of bound function. auto add2 = std::bind(add, std::placeholders::_1, 2); auto add3 = std::bind(add, std::placeholders::_1, 3)

QtCreator debugger: step into std::function

喜夏-厌秋 提交于 2020-01-16 05:28:04
问题 I am unable to find any tools within QtCreator that would allow me to debug std::function calls and determine what's happening inside. Is it possible to "step into" std::function ? In case it is possible, how do I set it up, because a strait-forward approach (i.e push "step into" button when paused on the corresponding std::function execution call) does not work. 来源: https://stackoverflow.com/questions/51589796/qtcreator-debugger-step-into-stdfunction

Is there something similar to std::function before C++11?

给你一囗甜甜゛ 提交于 2020-01-13 08:54:09
问题 What construct should be used as surrogate for std::function<> when C++11 is not available ? The alternative should basically allow to access private member functions of one class from another class like in the example below (other features of std::function are not used). Class Foo is fixed and can not be changed much, I have only access to class Bar. class Foo { friend class Bar; // added by me, rest of the class is fixed private: void doStuffFooA(int i); void doStuffFooB(int i); }; class

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

房东的猫 提交于 2020-01-11 12:30:12
问题 #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? 回答1: 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

Construct std::function with a constructor based on template

岁酱吖の 提交于 2020-01-06 08:32:04
问题 Is it possible to construct a std::function with the constructor of a type defined by a template argument? For example: template <typename T> bool registerType() { const std::function<T()> func = &T::T; //I know this doesn't work //... } 回答1: I don't think so, because constructors don't have names, you can't take a pointer/reference to them, and in general they don't behave quite like functions. You could use a lambda to initialize a std::function with the same signature: const std::function

C++ object keeps templated function and args as members to call later

匆匆过客 提交于 2020-01-04 15:54:23
问题 I have a class Door that implements a method LockCheck() , and a class Stove with a method BurnerCheck() . I want a class House that takes as a constructor argument either Door::LockCheck or Stove::BurnerCheck along with an unknown set of args for the given function. House would then store the function and its args such that it can call them at some later time. For example, auto stove = Stove(); auto stove_check = stove.BurnerCheck; auto burner_args = std::make_tuple<bool, bool>(true, false);

Alterantive for callbacks using std::function

别来无恙 提交于 2020-01-04 03:59:20
问题 Currently I am trying out a code that does essentially the following: void f(int x) { cout << "f("<<x<<")" << endl; } class C { public: void m(int x) { cout << "C::m("<<x<<")" << endl; } }; class C2 { public: void registerCallback(function<void(int)> f) { v.push_back(f); } private: vector<function<void(int)>> v; void callThem() { for (int i=0; i<v.size(); i++) { v[i](i); } } }; int main() { C2 registrar; C c; registrar.registerCallback(&f); // Static function registrar.registerCallback(bind(

std::list containing std::function

对着背影说爱祢 提交于 2020-01-02 10:25:19
问题 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

Why assignment to std::function<X()> doesn't compile when it is a member of class X?

拜拜、爱过 提交于 2019-12-31 12:39:07
问题 The following code doesn't compile: #include <functional> struct X { std::function<X()> _gen; }; int main() { X x; x._gen = [] { return X(); }; //this line is causing problem! } I don't understand why assignment to x._gen is causing problem. Both gcc and clang are giving similar error messages. Could anyone please explain it? Compiler error messages GCC's error: In file included from main.cpp:1:0: /usr/include/c++/4.8/functional: In instantiation of ‘std::function<_Res(_ArgTypes ...)>::