C ++ 17不能使用std :: bind来生成std :: function

时间:2018-05-18 14:44:17

标签: c++ functional-programming c++17 stdbind

我有一个Register函数,它以std::function<void(const uint8_t* data, size_t len)>为参数。我想使用对象的成员函数作为目标。

我发现this question根据其答案是使用std::bind将第一个第一个参数(隐式this指针)绑定到实际的对象指针,然后将其用作std::function论证。

然而,这在C ++ 11,C ++ 14和C ++ 17中都不再起作用了吗?

考虑以下测试程序。

#include <iostream>
#include <cstdint>
#include <functional>

void Register(std::function<void(const uint8_t* data, size_t len)> func) {
    //Dummy - directly call into function
    func(nullptr, 0);
}

class TestClass {
public:
    void TestRegister() {
         Register(
                 std::bind(&TestClass::TestTarget, this, std::placeholders::_1)
         );
    }

    void TestTarget(const uint8_t* data, size_t len) {
        (void) data;
        (void) len;
        std::cout << "Hello there" << std::endl;
    }
};


int main() {
    TestClass testObj;
    testObj.TestRegister();
    return 0;
}

在为-std=c++17进行编译时,会抛出一条相当神秘的错误消息(我不知道它在Wrong number of arguments for pointer-to-member处要说的是什么):

In file included from /home/max/Documents/TestingFunctions/main.cpp:3:0:
/usr/include/c++/7/functional: In instantiation of ‘struct std::_Bind_check_arity<void (TestClass::*)(const unsigned char*, long unsigned int), TestClass*, const std::_Placeholder<1>&>’:
/usr/include/c++/7/functional:854:12:   required from ‘struct std::_Bind_helper<false, void (TestClass::*)(const unsigned char*, long unsigned int), TestClass*, const std::_Placeholder<1>&>’
/usr/include/c++/7/functional:875:5:   required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (TestClass::*)(const unsigned char*, long unsigned int); _BoundArgs = {TestClass*, const std::_Placeholder<1>&}]’
/home/max/Documents/TestingFunctions/main.cpp:14:78:   required from here
/usr/include/c++/7/functional:841:7: error: static assertion failed: Wrong number of arguments for pointer-to-member
       static_assert(_Varargs::value
       ^~~~~~~~~~~~~
/home/max/Documents/TestingFunctions/main.cpp: In member function ‘void TestClass::TestRegister()’:
/home/max/Documents/TestingFunctions/main.cpp:14:26: error: could not convert ‘std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (TestClass::*)(const unsigned char*, long unsigned int); _BoundArgs = {TestClass*, const std::_Placeholder<1>&}; typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type = std::_Bind<void (TestClass::*(TestClass*, std::_Placeholder<1>))(const unsigned char*, long unsigned int)>](((TestClass*)this), std::placeholders::_1)’ from ‘std::_Bind_helper<false, void (TestClass::*)(const unsigned char*, long unsigned int), TestClass*, const std::_Placeholder<1>&>::type {aka std::_Bind<void (TestClass::*(TestClass*, std::_Placeholder<1>))(const unsigned char*, long unsigned int)>}’ to ‘std::function<void(const unsigned char*, long unsigned int)>’
                 std::bind(&TestClass::TestTarget, this, std::placeholders::_1)
                 ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

TestRegister函数替换为在lambda表达式中完全相同的函数编译并运行没有问题。

void TestRegister() {
    Register(
            [this](const uint8_t* data, size_t len) {
                TestTarget(data, len);
            }
    );
}

问题:为什么来自链接问题的std::bind方法不起作用?是否删除了此功能或我的代码中是否有错误?

2 个答案:

答案 0 :(得分:4)

您的函数Register需要一个带有两个参数的函数,但是您尝试将一个带有一个placeholded参数的函数传递给它。

void TestRegister() {
     Register(
             std::bind(&TestClass::TestTarget, this, std::placeholders::_1, std::placeholders::_2)
     );
}

答案 1 :(得分:2)

您的函数有两个参数,而您只传递一个占位符。

std::bind(&TestClass::TestTarget, this, std::placeholders::_1, std::placeholders::_2);