为什么这样做?

rad*_*man 2 c++ boost bind function

我刚刚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;

}
Run Code Online (Sandbox Code Playgroud)

Edr*_*ric 6

这不是重点boost::bind- 允许你重新映射函数的原型吗?您test可以使用6个输入参数,其中您的基础功能只需要3个.

这个页面:http://blog.think-async.com/2010/04/bind-illustrated.html非常好地概述了如何boost::bind工作.

  • @radman我和你在一起.我想我几乎更喜欢在这种情况下产生增强导致错误.我发现它比方便更令人不安. (2认同)