简体   繁体   English

C ++ 17无法使用std :: bind生成std :: function

[英]C++17 Cannot use std::bind to produce a std::function

I have a Register function which takes a std::function<void(const uint8_t* data, size_t len)> as a parameter. 我有一个Register函数,该函数将std::function<void(const uint8_t* data, size_t len)>作为参数。 I want to use the member function of an object as the target. 我想使用对象的成员函数作为目标。

I found this question according to which the answer is to use std::bind to bind the first first argument (the implicit this pointer) to the actual object pointer and then use it as the std::function argument. 我发现了这个问题,根据这个问题 ,答案是使用std::bind将第一个第一个参数(隐式this指针) std::bind到实际的对象指针,然后将其用作std::function参数。

This however doesn't work anymore in neither C++11, C++14 nor C++17? 但是,这在C ++ 11,C ++ 14和C ++ 17中都不起作用了吗?

Consider the following test program. 考虑以下测试程序。

#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;
}

When compiling for -std=c++17 this throws a rather cryptic error message (I have no idea what it's trying to say here with Wrong number of arguments for pointer-to-member ): 当为-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)
                 ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Replacing the TestRegister function with one that does the exact same thing in a lambda expression compiles and runs without problems. 用在lambda表达式中执行完全相同的操作替换TestRegister函数,即可编译并运行而不会出现问题。

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

Question: Why does the std::bind approach from the linked question not work? 问题:为什么链接问题中的std::bind方法不起作用? Was this feature removed or do I have an error in my code? 此功能是否已删除?代码中是否存在错误?

Your function Register expects a function with two parameters, but you try to pass to it a function with one placeholded parameter. 您的函数Register需要一个带有两个参数的函数,但是您尝试将一个带有一个占位参数的函数传递给它。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM