stdbind

Why do objects returned from bind ignore extra arguments?

孤者浪人 提交于 2019-11-27 15:00:54
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 more than one. The arguments beyond the first don't even have to be of a type that makes any sense: partiallyBoundF(20, 0); partiallyBoundF(0, 44, -99, "Hello", 4.5, true, []{}); What is the purpose of permitting objects returned from bind to be passed extra arguments? It allows calling errors to compile that would be rejected anyplace else. Ignoring

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

做~自己de王妃 提交于 2019-11-27 09:58:40
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 for newbies? std::bind is for partial function application . That is, suppose you have a function object f which takes 3 arguments: f(a,b,c); You want a new function object which only takes two arguments, defined as: g(a,b) := f(a, 4, b); g is a "partial application" of the function f : the middle argument has already been specified, and there are

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

余生长醉 提交于 2019-11-27 06:15:53
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? boost::bind has overloaded relational operators , std::bind does not. boost::bind supports non-default calling conventions , std::bind is not guaranteed to (standard library implementations may offer this as an extension). boost::bind provides a direct mechanism to allow one to prevent eager evaluation of nested bind expressions ( boost::protect ), std::bind does not. (That said, one can use boost::protect with std::bind if they

How std::bind works with member functions

主宰稳场 提交于 2019-11-27 03:59:48
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 code: auto fn_half = std::bind (my_divide,_1,2); // returns x/2 std::cout << fn_half(10) << '\n'; // 5 But now, with the following code where we have a bind to member function I have some questions. struct Foo { void print_sum(int n1, int n2) { std::cout << n1+n2 << '\n'; } int data = 10; }; Foo foo; auto f = std::bind(&Foo::print_sum, &foo, 95, _1); f(5);

Why do objects returned from bind ignore extra arguments?

旧巷老猫 提交于 2019-11-26 18:28:02
问题 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 more than one. The arguments beyond the first don't even have to be of a type that makes any sense: partiallyBoundF(20, 0); partiallyBoundF(0, 44, -99, "Hello", 4.5, true, []{}); What is the purpose of permitting objects returned from bind to be

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

 ̄綄美尐妖づ 提交于 2019-11-26 15:01:36
问题 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 for newbies? 回答1: std::bind is for partial function application. That is, suppose you have a function object f which takes 3 arguments: f(a,b,c); You want a new function object which only takes two arguments, defined as: g(a,b) := f(a, 4, b); g is a

Is there a reference_wrapper<> for rvalue references?

孤街浪徒 提交于 2019-11-26 14:22:39
问题 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 pass an rvalue reference and store it as an rvalue reference (possibly wrapped) in the call wrapper? I know I can manually write up a class like std::reference_wrapper<> that has a conversion function to T&& , but I would rather want to avoid that and use Standard

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

 ̄綄美尐妖づ 提交于 2019-11-26 11:55:14
问题 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? 回答1: boost::bind has overloaded relational operators, std::bind does not. boost::bind supports non-default calling conventions, std::bind is not guaranteed to (standard library implementations may offer this as an extension). boost::bind provides a direct mechanism to allow one to prevent eager evaluation of nested bind