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
5
votes
1 answer

omit std::placeholders in std::bind

To create std::function, here is what I do:- std::function f = std::bind(&B::fb,this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 ); void B::fb(int x,int k,int j){}…
javaLover
  • 6,347
  • 2
  • 22
  • 67
5
votes
1 answer

No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

I have a strange error I don't really understand, with VS2013. It's just a simplification of my real problem resulting in the same error. std::function x = (someCondition == true) ? []() { return true; } : []() { return false; }; VS…
Tiresias
  • 53
  • 1
  • 3
5
votes
2 answers

C++ class variable std::function which has default functionality and can be changeable

Need to have a function variable inside the class which have default functionality and it's functionality can be overwritten. Example how I liked/wanted to do(unfortunately unsuccessfully): #include #include using namespace…
Samps
  • 799
  • 6
  • 12
5
votes
4 answers

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

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…
DW_Ant
  • 61
  • 7
5
votes
2 answers

How is std::function constructed for a lambda

I am a bit confused by how std::function is constructed at a given lambda. The constructor of std::function is listed here. Which one is actually used to capture a lambda? Is it template< class F > function( F f );? Looks like I can't construct…
Jes
  • 2,614
  • 4
  • 25
  • 45
5
votes
3 answers

Variadic templates, type deduction and std::function

I'm trying to make a template function to which is possible to pass some other function with any type and number of parameters and bind it to a std::function. I managed to do this: #include #include int foo(int bar) { …
Tarc
  • 3,214
  • 3
  • 29
  • 41
5
votes
3 answers

Can a std::map contain a reference to a constructor?

Is there a way to point to a constructor from a std::map? I'd like to do the following, with the code I desire to use in #if 0, but I can't seem to get this to work: #include #include using namespace std; class Base { }; class…
firebush
  • 5,180
  • 4
  • 34
  • 45
5
votes
1 answer

How to give a member function as a parameter?

I am struggling with C++ templates, functions and bind. Let's say my class A is like this : class A { void set_enabled_for_item(int item_index, bool enabled); void set_name_for_item(int item_index, std::string name); int…
user1721413
5
votes
1 answer

Return type of function that returns an std::function

I have a function that should return an std::function of the same type as that function. Basically I want something like this: using RetType = std::function; which obviously will not compile. How do I correctly declare the return…
Sidharth Mudgal
  • 4,234
  • 19
  • 25
5
votes
2 answers

Constructing std::function target in-place

What I understand as a typical use of std::function #include #include using namespace std; class C { public: C() { cout << "CREATING" << endl; } C(const C&) { cout << "COPY C "<< endl; }; C(C&&) { cout << "MOVE…
Smiles
  • 1,733
  • 2
  • 11
  • 13
5
votes
2 answers

std::function vs function pointer

Is there any differences? Which is the best way to "save/transfer" function? function fcn = [](int par) {std::cout<<"fcn: "<
Person.Junkie
  • 1,816
  • 4
  • 21
  • 31
5
votes
2 answers

Which function prototype is invoked (and how) when assigning a function to a std::function type?

I have the code void prints_one() { cout << "one" << endl; } int main(int argc, char *argv[]) { std::function foo; foo = prints_one; foo(); return 0; } It works as expected; it prints "one". What I don't know is which…
Mihir
  • 53
  • 4
5
votes
2 answers

How to get this pointer from std::function?

Since std::function can hold member functions, so it must store a pointer to the object instance somewhere. How can I fetch the this pointer from a std::function that holds a member function?
danijar
  • 32,406
  • 45
  • 166
  • 297
5
votes
6 answers

A parallel for using std::thread?

I'm new with std::thread and I try to code a parallel_for. I coded the following thing: // parallel_for.cpp // compilation: g++ -O3 -std=c++0x parallel_for.cpp -o parallel_for -lpthread // execution: time ./parallel_for 100 50000000 // (100: number…
Vincent
  • 57,703
  • 61
  • 205
  • 388
5
votes
1 answer

Construct std::function with a constructor based on template

Is it possible to construct a std::function with the constructor of a type defined by a template argument? For example: template bool registerType() { const std::function func = &T::T; //I know this doesn't work //... }
Graeme
  • 4,514
  • 5
  • 43
  • 71