The functor returned by std::bind
ignores superfluous arguments [SO1, SO2, SO3]. A simple example looks as follows.
#include <iostream>
#include <functional>
void foo(int a) {
std::cout << "a=" << a << std::endl;
}
int main() {
auto f = std::bind(foo, 42);
f(3141); // a=42
}
It is clear that implementing std::bind
in this way is easier [SO1a]. Is, however, this ignoring of superfluous arguments standardized or undefined behavior?
std::bind. The function template bind generates a forwarding call wrapper for f . Calling this wrapper is equivalent to invoking f with some of its arguments bound to args .
std::bind is a Standard Function Objects that acts as a Functional Adaptor i.e. it takes a function as input and returns a new function Object as an output with with one or more of the arguments of passed function bound or rearranged.
Bind function with the help of placeholders helps to manipulate the position and number of values to be used by the function and modifies the function according to the desired output. What are placeholders? Placeholders are namespaces that direct the position of a value in a function.
boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.
Yes, for std::bind
, the superfluous arguments will be discarded.
If some of the arguments that are supplied in the call to g() are not matched by any placeholders stored in g, the unused arguments are evaluated and discarded.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With