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.

844 questions
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
  • 7,118
  • 9
  • 61
  • 108
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
  • 9,465
  • 8
  • 44
  • 75
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,660
  • 4
  • 28
  • 44
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
  • 57,703
  • 61
  • 205
  • 388
-1
votes
2 answers

std::remove_if() problem with the for looping

my code was #include #include #include using namespace std; void tes(std::string s) { cout << "tes " << s << '\n'; } void tes2(std::string s) { cout << "tes " << s << '\n'; } void tes3(std::string s) { …
lie ardi
  • 5
  • 2
-1
votes
1 answer

When I use std::function, I got a mistake of receiving the signal SIGABRT. Why?

I want to use submit to construct a function, which can be used in other threads. And In order not to be blocked, submit will return std::future, which can let me get the return value in the proper time. My code is…
-1
votes
1 answer

Casting the std::function to the C function pointer

I have a problem with casting: the std::function to the typedef int32_t (*callback_c_type) (std::string &, uint32_t (C function pointer). My full example: https://onlinegdb.com/5KI36oPlQ #include…
Michał Hanusek
  • 199
  • 3
  • 13
-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
  • 761
  • 4
  • 14
-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
  • 87
  • 8
-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
  • 97
  • 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
  • 3
-1
votes
2 answers

std::function as a parameter in delegate constructor

I want to know why control does not go to AB() if I pass in abc(AB) into 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
  • 2,111
  • 4
  • 22
  • 42
-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,964
  • 1
  • 22
  • 22