为什么std :: bind不适应函数签名?

时间:2016-11-01 20:42:32

标签: c++

我要把头发撕掉。我不知道我做错了什么或从根本上误解了std::bind的用途。我叫这个函数:

void fetch(Face& face,
           const Interest& baseInterest,
           shared_ptr<Validator> validator,
           const CompleteCallback& completeCallback,
           const ErrorCallback& errorCallback);
像这样:

ndn::util::SegmentFetcher::fetch(
  m_nlsrFace,
  interest,
  m_validator,
  [&datasetBlob](const ndn::ConstBufferPtr& data){
    datasetBlob = data;
  },
  std::bind(&Nlsr::onFaceDatasetFetchTimeout,
            _1, _2, datasetBlob, interest));

ErrorCallback的签名是这样的:

typedef function<void (uint32_t code, const std::string& msg)> ErrorCallback;

正在抛出的错误是:

/usr/local/include/ndn-cxx/util/segment-fetcher.hpp:141:3: note:
  no known conversion for argument 5 from ‘
    std::_Bind_helper<
      false,
      void (nlsr::Nlsr::*)(unsigned int,
                           const std::basic_string<char>&,
                           const std::shared_ptr<const ndn::Buffer>&,
                           ndn::Interest),
      const std::_Placeholder<1>&,
      const std::_Placeholder<2>&,
      std::shared_ptr<const ndn::Buffer>&,
      ndn::Interest&>::type
  {aka std::_Bind<std::_Mem_fn<void (nlsr::Nlsr::*)(
                                 unsigned int,
                                 const std::basic_string<char>&,
                                 const std::shared_ptr<const ndn::Buffer>&,
                                 ndn::Interest)>
                    (std::_Placeholder<1>,
                     std::_Placeholder<2>,
                     std::shared_ptr<const ndn::Buffer>,
                     ndn::Interest)>}’
  to ‘const ErrorCallback&
  {aka const std::function<void(unsigned int, const std::basic_string<char>&)>&}’

我只是不明白这个问题是什么。我绑定函数以获得正确数量的参数(也是正确的类型!)并修复其他参数。但似乎std::bind没有改变功能签名,因为我似乎遇到类型解析问题。那么std::bind的用途是什么?我使用不正确?

1 个答案:

答案 0 :(得分:3)

由于Nlsr::onFaceDatasetFetchTimeout为非static,您必须将Nlsr的实例绑定到它(在第一个参数处)。

std::bind(&Nlsr::onFaceDatasetFetchTimeout,
          myNlsrObject,
          _1, _2, datasetBlob, interest)