Questions tagged [stdbind]

The C++11 function std::bind() fixes some or all arguments of a function object, returning another function object that takes fewer arguments.

std::bind performs Partial Function Application of arbitrary C++ function objects, fixing some or all arguments of a function object and returning another function object that takes fewer arguments.

The function is based on boost::bind() which originated in the library (see ) and an earlier version was included in as std::tr1::bind().

371 questions
2
votes
1 answer

std::bind null function pointer

#include #include typedef int(*SumFptr)(int,int); int main() { SumFptr ptr = nullptr; std::function func = std::bind(ptr, std::placeholders::_1, 42); std::cout << (func ? "true" : "false") <<…
stima
  • 131
  • 6
2
votes
4 answers

Issue in passing argument to std::function for vector of functions

I'm trying to create a vector of std::function and then pass that vector to a function. I also need to pass arguments to the function objects, so I'm using std::bind. Here is the code: #include #include #include…
DKR
  • 53
  • 7
2
votes
2 answers

Using std::function and bind to assign functions with different argument lists

I'm trying to have a function pointer where, in some cases, it either is assigned a function takes in 2 arguments (a cv::Mat and a struct that contains parameters) or a different function that takes 3 arguments (the same 2 arguments and a list of…
Hal T
  • 527
  • 7
  • 22
2
votes
2 answers

Managing the lifetime of member functions bound by `std::bind`

I am currently experimenting with writing an event queue in C++11. I am using std::bind to obtain std::function objects which are called when certain events happen. The code for this roughly looks like this: class A { public: void…
Gnosophilon
  • 1,340
  • 7
  • 24
2
votes
2 answers

Storing the result of a bind with placeholders in a std::function

I have been reading up on, how to perform a std::bind on a regular function. And store the free function or member function into a std::function. However, if I try to use a placeholder for one argument and an actual value for the other argument; I…
thassan
  • 391
  • 3
  • 15
2
votes
2 answers

Calling std::string method using std::bind

I need call a std::string method member using std::bind, I tried the follow, but it gave an error: #include #include #include int main(int argc, char **argv) { std::string test = "fdsa"; auto fn =…
Alex
  • 3,301
  • 4
  • 29
  • 43
2
votes
1 answer

Function to bind member functions to object instances in C++

Recently, I've frequently been binding member functions to instances of objects. Rather than using a combination of std::bind, std::mem_fn and std::ref, I'd like to combine all of these into one function to handle it all automatically. For example,…
Charles Ofria
  • 1,936
  • 12
  • 24
2
votes
1 answer

std::bind on member function with more than one argument

I have this code struct A { void f(int) {} void g(int, double) {} }; int main() { using std::placeholders; A a; auto f1 = std::bind(&A::f, &a, _1); f1(5); // <--- works fine auto f2 =…
user1738687
  • 457
  • 3
  • 12
2
votes
1 answer

C++ - How to correctly bind a default_random_engine to two different uniform_int_distributions

I am trying to use two different objects of std::uniform_int_distribution bound (using std::bind) with the same object std::default_random_engine as argument (as described here http://www.cplusplus.com/reference/random/), but binding them together…
Yannik
  • 45
  • 5
2
votes
1 answer

Why are placeholders required in std::bind in this case?

While answering this question, I see the following fact by accident. Please see this example: void func1(const char *str1, const char *str2) { puts(str1); puts(str2); } ... auto fn = std::bind(func1, "asdf"); fn("1234"); It is failed to compile…
ikh
  • 10,119
  • 1
  • 31
  • 70
2
votes
1 answer

use of std::bind and std::function

EDIT3: If I delete the second createTriangle function, it works. So how can I bind overloaded functions? I have a function which takes a function object with one parameter, like this: int createObject(std::function); How can I call this…
gartenriese
  • 4,131
  • 6
  • 36
  • 60
2
votes
1 answer

Delayed Function Call in a Delayed Function Call

Basically what I want to do is this: std::function< int( void ) > foo = &getInt; int magicNumber = 13; std::function< int( void ) > delayedAdd = std::bind( std::plus, magicNumber, getInt ); Clearly this won't work. I need to somehow wrap the getInt…
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
2
votes
2 answers

c++11 bind and apply?

std::bind is sometimes described as "partial application". Any reasons why when all parameters of a function are bound, the function itself isn't applied? For example, the following code prints nothing. #include #include…
thor
  • 21,418
  • 31
  • 87
  • 173
2
votes
2 answers

Binding using std::bind vs lambdas. How expensive are they?

I was playing with bind and I was thinking, are lambdas as expensive as function pointers? What I mean is, as I understand lambdas, they are syntactic sugar for functors and bind is similar. However, if you do…
Adrian
  • 10,246
  • 4
  • 44
  • 110
2
votes
1 answer

Use std::bind to bind to the parent's version of a virtual function

I want to create a std::function object for the parent class's version of a virtual and overridden function, see the following example: #include #include class Parent { public: virtual void func1() { …