What are the benefits of std :: bind allowing and discarding extra arguments?

Consider this code,

#include <iostream> 
#include <functional>

void pacifist() 
{    
   std::cout << "I don't get involved in arguments. I'm a pacifist.\n";
}

int main() {

   auto x = std::bind(pacifist);

   x();           //OK: Makes perfect sense. No argument, being a pacifist!
   x(5);          //OK: but WTF!
   x(5, 10);      //OK: but WTF!!
   x(5, 10, 15);  //OK: but WTF!!!

}

And it compiles fine; It even starts, but all calls are xcalls pacifist! ( online demo ):

$ clang ++ -std = C ++ 14 -O2 -Wall -pedantic -pthread main.cpp && & & &. / a.out

I don't get involved in arguments. I'm a pacifist.
I don't get involved in arguments. I'm a pacifist.
I don't get involved in arguments. I'm a pacifist.
I don't get involved in arguments. I'm a pacifist.    

Why is it std::binddesigned to work with arguments in this case? Do they even make sense? Don't they mislead programmers? It works with g ++ .

std::bind the documentation in cppreference says

If some arguments provided in the g () call are not matched with any placeholders stored in g, unused arguments are evaluated and discarded.

, ? ? .

, :

void call(std::function<void(int)> fun) 
{
   fun(5);
}

call(std::bind(pacifist)); //OK: again WTF!

, , . , std::function<void(int)> : " int" , , int; , , int.

+4

Source: https://habr.com/ru/post/1686930/


All Articles