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
178
votes
4 answers

std::function and std::bind: what are they, and when should they be used?

I know what functors are and when to use them with std algorithms, but I haven't understood what Stroustrup says about them in the C++11 FAQ. Can anyone explain what std::bind and std::function are, when they should be used, and give some examples…
Mr.Anubis
  • 5,132
  • 6
  • 29
  • 44
73
votes
4 answers

Difference between C++11 std::bind and boost::bind

Is there any difference between the two? Or am I safe to replace every occurrence of boost::bind by std::bind in my code and thereby remove the dependence on Boost?
Haatschii
  • 9,021
  • 10
  • 58
  • 95
64
votes
2 answers

Why use `std::bind_front` over lambdas in C++20?

As mentioned in a similarly worded question (Why use bind over lambdas in c++14?) The answer was - no reason (and also mentioned why it would be better to use lambdas). My question is - if in C++14 there was no longer a reason to use bind, why did…
feature_engineer
  • 1,088
  • 8
  • 16
47
votes
2 answers

How std::bind works with member functions

I'm working with std::bind but I still don't get how it works when we use it with member class functions. If we have the following function: double my_divide (double x, double y) {return x/y;} I understand perfectly well the next lines of…
Tarod
  • 6,732
  • 5
  • 44
  • 50
24
votes
2 answers

Why use mem_fn?

I'm confused as to why std::mem_fn is needed. I have a function that takes in any callable (lambda, function pointer, etc),and binds it to an argument. Eg: template void Class::DoBinding(T callable) { m_callable = std::bind(callable, _1,…
Jonathan.
  • 53,997
  • 54
  • 186
  • 290
22
votes
2 answers

Why is std::bind not working without placeholders in this example (member function)?

For example, this is my member function (do_it): class oops { public: void do_it(GtkWidget *widget, GdkEvent *event, gpointer data) { g_print ("Hi there :)\n"); } }; ... and i use std::bind to make it look like a non-member…
Sadeq
  • 7,795
  • 4
  • 34
  • 45
22
votes
1 answer

Why do objects returned from bind ignore extra arguments?

Suppose I have a function that takes two arguments, void f(int x, int y); and I want to bind one of them. I can use std::bind as follows: auto partiallyBoundF = std::bind(f, 10, _1); partiallyBoundF takes only one argument, but I can call it with…
KnowItAllWannabe
  • 12,972
  • 8
  • 50
  • 91
21
votes
2 answers

std::bind lose reference when delivered as rvalue reference

I have the following code: #include #include template auto callback(T&& func) ->decltype(func()) { return func(); } double test(double& value) { value=value+1.0; return value; } int main(void) { …
xis
  • 24,330
  • 9
  • 43
  • 59
19
votes
2 answers

How to bind function to an object by reference?

I have the following code to bind a member function to an instance of the class: class Foo { public: int i; void test() { std::cout << i << std::endl; } }; int main() { Foo f; f.i = 100; auto func =…
SwiftMango
  • 15,092
  • 13
  • 71
  • 136
17
votes
4 answers

Is there a reference_wrapper<> for rvalue references?

I wonder how the following can be done void f(string &&s) { std::string i(move(s)); /* other stuff */ } int main() { std::string s; bind(f, s)(); // Error. bind(f, move(s))(); // Error. bind(f, ref(s))(); // Error. } How can I…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
16
votes
1 answer

std::bind and overloaded function

Please refer the following code snippet. I want to use the std::bind for overloaded function foobar. It calls only the method with no arguments. #include #include class Client { public : void foobar(){std::cout << "no…
Atul
  • 745
  • 3
  • 9
  • 17
15
votes
2 answers

Bind move-only structure to function

I need to bind structure with deleted copy-constructor to a function. I have reduced what I am trying to achieve into following minimal example: struct Bar { int i; Bar() = default; Bar(Bar&&) = default; Bar(const Bar&) = delete; …
Jendas
  • 3,359
  • 3
  • 27
  • 55
14
votes
4 answers

Why start with std::placeholders::_1 instead of _0?

Most everything in c++ is 0, not 1 based. Just out of curiosity, why are placeholders 1 based? Meaning _1 is the first parameter, not _0.
walrii
  • 3,472
  • 2
  • 28
  • 47
13
votes
2 answers

Opposite of std::bind, pass in different functions for given parameters

I have several functions with almost identical signatures (made much shorter than actual code): int hello(A a, B b, C c, int n); int there(A a, B b, C c, int n); int how(A a, B b, C c, int n); int are(A a, B b, C c, int n); ... And so on. Then…
jeanluc
  • 1,608
  • 1
  • 14
  • 28
13
votes
3 answers

What the heck does std::bind(x, y) do?

I'm stuck reading the description of std::bind in N3225, in subsection 20.8.10.1. It says the following should print 1, but I thought that bind is supposed to copy its arguments and therefor it should print 0. If one wants to refer to the passed…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
1
2 3
24 25