6

After dealing with some weird stuff yesterday, the cause of my error was that std::bind can take more arguments than it needs to make the call

int f(int);
auto b = std::bind(f);
b(1,2,3,4,5,6); // 2-6 are discarded

cppreference claims:

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.

Searching through the standard I don't see anything that supports this. The only thing that possibly makes this okay is:

20.9.2/4 A forwarding call wrapper is a call wrapper that can be called with an arbitrary argument list and delivers the arguments to the wrapped callable object as references

because, (I suppose) it doesn't say "delivers all the arguments" but I'm unconvinced.

My "Why" in this question consists of two parts

  1. What reading of the standard makes it clear that std::bind is allowed to discard arguments?

  2. Why is this desirable behavior? It feels very not-c++ to me. I can't think of a non-contrived situation where I'd want this, but can think of times it would be a problem. Does it somehow make the implementation easier in a way I'm not seeing?

Community
  • 1
  • 1
Ryan Haining
  • 35,360
  • 15
  • 114
  • 174
  • 1
    _"What reading of the standard makes it clear that `std::bind` is allowed to discard arguments?"_ What reading of the standard makes it clear that it isn't? And how would you implement that? – Lightness Races in Orbit Jul 02 '15 at 16:30
  • 2
    Related: http://stackoverflow.com/q/13251976/ – dyp Jul 02 '15 at 16:34
  • I think Mr. Wakely's answer on the linked question deals with both of your questions pretty well. – Barry Jul 02 '15 at 16:36
  • @LightnessRacesinOrbit at the time the object is created, it should know how many placeholders it has. If more arguments are passed to the call operator than there are placeholders, that would indicate a misuse would it not? – Ryan Haining Jul 02 '15 at 16:55
  • @dyp: Exact dupe, rather – Lightness Races in Orbit Jul 02 '15 at 17:17
  • @LightnessRacesinOrbit I don't see the first question answered in the Q/A I've linked. Barry's dup doesn't answer this Q on a language-lawyer level either, IMO. – dyp Jul 02 '15 at 17:28
  • @dyp: Well, this is why we ask for _one question per question_ and I reserve the right to selectively ignore addendum questions when provided. :) – Lightness Races in Orbit Jul 02 '15 at 17:44

0 Answers0