views:

76

answers:

2
#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.

A: 

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

Brooks Moses
Nope. The argument count has a fixed, [implementation-defined limit](http://www.boost.org/doc/libs/1_43_0/libs/bind/bind.html#NumberOfArguments). The current limit is nine.
Rob Kennedy
+2  A: 

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.

SCFrench
I'd still love to know the reasoning for why to allow this behavior if anyone finds out :)
Joseph Garvin