Why can a Boost.Bind function be called with extra parameters?
Asked Answered
P

2

7
#include <iostream>
#include <string>

#include <boost/bind.hpp>

void foo(std::string const& dummy)
{
    std::cout << "Yo: " << dummy << std::endl;
}

int main()
{
    int* test;
    std::string bar("platypus");
    (boost::bind(&foo, bar))(test, test, test, test, test, test, test, test);
}

When run, it prints out, "Yo: platypus." It appears to completely ignore extra parameters. I'd expect to get a compile error. I accidentally introduced a bug into my code this way.

Pacifier answered 23/7, 2010 at 18:15 Comment(0)
V
2

I don't know why this is allowed, but I do know it is expected behavior. From here:

bind can handle functions with more than two arguments, and its argument substitution mechanism is more general:

bind(f, _2, _1)(x, y);                 // f(y, x)
bind(g, _1, 9, _1)(x);                 // g(x, 9, x)
bind(g, _3, _3, _3)(x, y, z);          // g(z, z, z)
bind(g, _1, _1, _1)(x, y, z);          // g(x, x, x)

Note that, in the last example, the function object produced by bind(g, _1, _1, _1) does not contain references to any arguments beyond the first, but it can still be used with more than one argument. Any extra arguments are silently ignored (emphasis mine), just like the first and the second argument are ignored in the third example.

Vaporize answered 24/7, 2010 at 1:34 Comment(2)
I'd still love to know the reasoning for why to allow this behavior if anyone finds out :)Pacifier
@Joseph: Likely just too complex to explicitly disallow it.Cleavland
L
0

I bet it's creating the bound function as a variadic function, like printf.

Longfaced answered 23/7, 2010 at 18:24 Comment(1)
Nope. The argument count has a fixed, implementation-defined limit. The current limit is nine.Mayce

© 2022 - 2024 — McMap. All rights reserved.