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
5
votes
3 answers

How does std::bind Results in calling the Copy Constructor Several Times

I have been trying to understand how std::bind works. So working up with different examples. Below is the sample program whose output i am unable to understand. VERSION 1 class NAME { public: void f() { std::cout<<"f"<
Jason
  • 36,170
  • 5
  • 26
  • 60
5
votes
4 answers

What is the right way to pass an object created by std::bind to a function?

Let's assume I want to pass a function object created by std::bind by reference to a funktion: void myCallback(int i, int j) { std::cout << "toCall , i=" << i << " j=" << j; } void worker(std::function & callback) { …
MichaelW
  • 1,328
  • 1
  • 15
  • 32
5
votes
1 answer

How do I bind arguments to a constructor?

As described above, I want to use std::bind to create a function that, when called, returns an object built with a constructor with default parameters like below: #include class X { int x_, y_; public: X(int x, int y):…
GHJGHJJHG
  • 108
  • 1
  • 6
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
5 answers

Is there an equivalent of std::bind in javascript or node.js?

This is a long shot, but I was wondering if there is such a thing as the C++ std::bind in javascript or node.js? Here's the example where I felt the need for a bind: var writeResponse = function(response, result) { …
cocheci
  • 375
  • 3
  • 7
5
votes
2 answers

How do I use bind to pass a member function as a function pointer?

I'm trying to pass a member function as a function pointer so that I don't need to rely on singletons or global functions to handle Qt messages in Qt 5. As far as I can tell my std::function is of the correct type, it has the correct signature, and…
Troyseph
  • 4,960
  • 3
  • 38
  • 61
5
votes
3 answers

Clang: Trouble using bind or mem_fn with string::c_str and transform

Trying to convert a vector of std::string to a vector of const char*: #include #include #include #include int main(int argc, char** argv) { std::vector values; …
Ryan
  • 231
  • 1
  • 14
5
votes
3 answers

How to std::bind() to create a data member?

I'm generating random values with C++11 nice new generators and distributions. In a function it works like a charm and looks like this: void foo() { mt19937 generator; uniform_int_distribution distribution; auto dice =…
cxxl
  • 4,939
  • 3
  • 31
  • 52
5
votes
1 answer

Passing in function pointer as argument to another function in std::bind

Im trying to wrap a passed in function inside a try-catch clause so that I can catch exceptions thrown by it and do some cleanup before re-throwing. I have written sample code that replicates my compile bug. #include #include…
sibtx13
  • 123
  • 1
  • 6
4
votes
1 answer

Semantics of std::bind and/or std::forward

I find it very confusing that the following code fails to compile #include class Mountain { public: Mountain() {} Mountain(const Mountain&) = delete; Mountain(Mountain&&) = delete; ~Mountain() {} }; int main () { Mountain…
4
votes
2 answers

Functional composition with std::bind

I am reading the second edition of the beautiful book of Nicolai Josuttis on C++11 STL. I found the following piece of code: #include #include int main() { auto plus10 = std::bind(std::plus(), …
fabiop
  • 179
  • 10
4
votes
1 answer

callbacks fails when std::bind and std::function used

There are two class server and client. client calls method datas() on server. The server responds to the caller with the datalist using datacallback(). I see compile time error in function clientfun2 when serverobj.datas() is called. Please help to…
josp
  • 97
  • 5
4
votes
2 answers

std::bind with variadic template function

I'm trying to write a generic obj factory using variadic template to call constructors of various classes. code as follow: #include #include #include #include #include template
Charles
  • 41
  • 2
4
votes
2 answers

C++17 Cannot use std::bind to produce a std::function

I have a Register function which takes a std::function as a parameter. I want to use the member function of an object as the target. I found this question according to which the answer is to use std::bind to…
Maximilian Gerhardt
  • 5,188
  • 3
  • 28
  • 61
4
votes
1 answer

Compare std::function created with std::bind

Here is a toy example #include #include struct Obj { int x; int foo() { return 42; } }; int main() { Obj a, b; std::function f1 = std::bind(&Obj::foo, &a); std::function f2 =…
valentin
  • 2,596
  • 6
  • 28
  • 48