Bronze Bronze - 1 month ago 14
C++ Question

Why does std::bind not adapt function signatures?

I'm about to tear my hair out. I don't know if I'm doing something wrong or fundamentally misunderstanding what

std::bind
is for. I am calling this function:

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


like this:

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


And the signature for
ErrorCallback
is like this:

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


The error that is being thrown at me is this:

/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>&)>&}’


I just don't understand what the issue is. I am binding the function to have the right number of parameters (and of the right type, too!) and fixing the others. But it seems that
std::bind
doesn't change the function signature, because I appear to be having issues with type resolution. What then is the use of
std::bind
? I am using it incorrectly?

Answer

As Nlsr::onFaceDatasetFetchTimeout is non static, you have to bind the instance of Nlsr to it (at first argument).

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