Questions tagged [std-function]

A C++11 class template that is callable like a function, and wraps another callable type and forwards calls to it.

In the words of the C++11 standard std::function is a polymorphic wrapper class that encapsulates arbitrary callable objects. It is polymorphic because an instance of the type std::function<R (A1, A2)> could wrap an object of many different callable types, including:

  • a function pointer of type R (*)(A1, A2)
  • a function pointer of type R (*)(const A1&, const A2&)
  • a function object (a.k.a functor) with a member function such as R C::operator()(A1, A2)
  • a pointer to member function of type R (A1::*)(A2)
  • a lambda [](A1 a1, A2 a2) -> R {...}

std::function is often used to implement generic callbacks or to support passing arbitrary callable types to a function that cannot be written as function template e.g. because it must be virtual.

Use this tag for questions about std::function and std::tr1::function.

705 questions
0
votes
1 answer

c++ passing std::function from another class to a constructor

I am passing a method into a constructor, but when I used a method that another class, it gives me an error. It is currently working in this form; unsigned int pHash(const std::string& str) { //method } int main() { //stuff HashMap* hm2 = new…
Richard Lee
  • 652
  • 8
  • 19
0
votes
3 answers

Why is the ternary operator not the same as an if-else here?

I'm using TR1's std::function to implement a simple callback mechanism. If I don't want to get called back, I register nullptr as the callback handler. This compiles and works fine: void Foo::MessageHandlingEnabled( bool enable ){ if( enable ) …
dario_ramos
  • 6,536
  • 7
  • 50
  • 104
0
votes
2 answers

'Incomplete type' error declaring pointer-to-member function template parameter for std::function

Here's the declaration: #include class A { ... }; double fA( std::function fp) { ... } which gives me error In function ‘double fA(std::function)’: tb.cpp:32:8: error: ‘fp’ has incomplete type Though…
Matt Phillips
  • 8,691
  • 8
  • 41
  • 72
0
votes
0 answers

std::function callback in libc++

Possible Duplicate: Does std::function's copy-constructor require the template type's argument types to be complete types? I have a simple class with a callback built on std::function, but it's not building with C++11 / libc++. I think I see the…
rich.e
  • 3,302
  • 4
  • 23
  • 41
0
votes
2 answers

C++ : Functors and std::function for a noob

I have a simple problem but I don't know how to solve it because I have never used functors in C++. I want to do something like that (it is just an example) : class MyClass { void applyFunction(myFunction); /* WRONG SYNTAX */ double *_x; …
Vincent
  • 50,257
  • 51
  • 171
  • 339
-1
votes
1 answer

std::function with alias declaration gives incomplete type compiler error

I was playing with std::function and encountered behavior that I don't understand. I was hoping someone could explain to me what's going on in the code at the bottom of the question. Providing function type directly of course works. Simiralry with…
Lehu
  • 669
  • 4
  • 13
-1
votes
3 answers

Why does std::function can implicit convert to a std::function which has more parameter?

I have the following: void print_str(std::shared_ptr str) { std::cout << str->c_str() << std::endl; } int main() { auto str = std::make_shared("Hello"); std::function f = std::bind(print_str, str); …
Griyn
  • 57
  • 7
-1
votes
2 answers

Map function class member inside class itself

I have difficulty to map the functions of the class member inside the class itself #include #include #include class Foo{ public: void bar() {} void jar() {} private: std::map
Lele Mabur
  • 65
  • 6
-1
votes
1 answer

Is it possible for a std::function to return a value?

So for a project I'm currently working on, I'm attempting to create a sort of "Function lookup table", basically a map of strings to functions. The function takes in data, modifies it, and then spits it back out. However, it doesn't seem possible…
the4naves
  • 81
  • 7
-1
votes
1 answer

How can I access std::function in std::list in C++

I am attempting to answer generate a Template function in C++ which takes in an std::list of std::function (I think). I am however not sure how to understand the datatype I am working with. According to GDB my datatype is: type =…
-1
votes
1 answer

Callback function in QThread Class

I have a QThread based class, basically a GUI thread. In this thread I'm using another class which have this function type definition: void SomFunc(const std::function &data) I want to create a callback function in my…
zawarasal
  • 11
  • 2
-1
votes
2 answers

std::function as a parrameter in delegate constructor

i want to know why control does not goes to AB() if i passed abc(AB) in main() as control goes for initialization of i as 10 when abc(10) is passed class abc { int i; std::functionfunc = nullptr; public: abc(){} …
-1
votes
2 answers

How to use callbacks in c++?

I am trying to use callbacks in c++ using std::function. I have two files, mainwindow.cpp and tcpclient.cpp. A member function of mainwindow is passed to tcpclient in order call the passed function when a certain even occurs. mainwindow.h namespace…
Vino
  • 1,685
  • 2
  • 15
  • 34
-1
votes
3 answers

Questions about the definition of std::function class

I saw its source code(Mac XCode, /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/functional) is template
Robert
  • 1,419
  • 1
  • 20
  • 20
-1
votes
2 answers

Using bind inside of function call

Ok so I already have this as a working example but I'm just trying to clean it up. currently I have a subscription styled event system with callback routines. I have no problem with adding regular functions and class member functions into the…
Steven Venham
  • 526
  • 1
  • 3
  • 16
1 2 3
46
47