Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is std::bind ignoring superfluous arguments standard-conforming?

Tags:

c++

c++11

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?

like image 351
H. Rittich Avatar asked Jul 27 '21 13:07

H. Rittich


People also ask

What does std :: bind do?

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 .

What does STD bind return?

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.

Why BIND is used in C++?

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.

What is boost bind?

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.


1 Answers

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.

like image 76
songyuanyao Avatar answered Oct 18 '22 03:10

songyuanyao