4

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 runs —yes all invocations of x calls 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 std::bind designed to work with arguments in this case? Do they even make sense? Are they not misleading for programmers? It works with g++ as well.

The std::bind documentation at cppreference says,

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.

But it doesn't explain why is it designed to be so? What is the advantage? I see only disadvantages.

It is difficult to test and debug, which becomes more difficult in cases like this:

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

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

But you might think, this wont compile, but it does and runs as well. Now it seems even more misleading because std::function<void(int)> says "I take one int argument" and thus it passes an int; but the function stored eventually doesn't take any int at all.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 4
    Why downvote? Why close? That too, within `2` seconds? That means, without even reading the question, someone has downvoted and also voted for close? – Nawaz Oct 05 '17 at 05:07
  • The only reason I can think of for this "feature" is that bind expressions aren't usually evaluated like this, but used to instantiate e.g. std::functions or more complex bind expressions. But that is just me speculating. – juanchopanza Oct 05 '17 at 05:12
  • 3
    Not me, but I think someone may have thought your title was snarky. That's just a guess. Consider an appropriate title change: e.g. `Why does std::bind work when argument count doesn't match?` Something like that. – selbie Oct 05 '17 at 05:12
  • How is this opinion based? It is asking why the function returned by bind typechecks, I suspect the reason is technical. (As in it would have been more typesafe if it could have been at the time) – Aluan Haddad Oct 05 '17 at 05:13
  • @juanchopanza: See this example with [std::function](http://coliru.stacked-crooked.com/a/6be6311506f2dbe6). I dont see any advantage here as well. – Nawaz Oct 05 '17 at 05:20
  • 1
    I downvoted. I did read the question before DV, and I understood it. I thought it wasn't a good question. Upon further thinking and some research, I began to question *why* I had downvoted. I think I'm not thinking that straight at this moment. While I don't feel that this question is *that* good, it's certainly not bad. The close vote was definitely a mistake. Sorry for any confusion – Justin Oct 05 '17 at 05:21
  • 1
    @Justin: If it is not good as per you, make it good by editing. There is "edit" functionality as well, besides "downvote" and "close". ;-) – Nawaz Oct 05 '17 at 05:37
  • 1
    I didnt vote but the question sounds a bit angry (as opposed to being in neutral tone) – M.M Oct 05 '17 at 07:20

0 Answers0