我刚刚与boost::bind
和boost::function
一起工作,并注意到以下行为(我认为这有点奇怪)。您可以使用比boost :: function类型所需的参数更少的参数来绑定一个函数!看起来似乎任何附加的参数都被忽略,并且会消失。这为什么有效?
那么为什么这种行为是正确的?我的期望是应该提出编译错误,说明不兼容性。
请参阅以下工作代码示例,说明问题
#include "boost/bind.hpp"
#include "boost/function.hpp"
namespace
{
int binder(const char& testChar,
const int& testInt,
const std::string& testString)
{
return 3;
}
}
int main(int c, char** argv)
{
boost::function<int(const char&,
const int&,
const std::string&,
const float&,
const std::string&,
const int&)> test;
test = boost::bind(&::binder, _1, _2, _3);
std::cout << test('c', 1, "something", 1.f, "more", 10) << std::endl;
}
我不确定这是否是“正常”,但我想是的。 'Qt'信号和插槽也可以。 – ereOn 2010-06-21 07:18:21