I tried to bind the function of class B to the callback of class A, so I used std::bind() with two param, this and rvalue. And I get funtion<void()> object, maybe, but conversion error in gcc-11.
#include <functional>
#include <iostream>
#include <string>
using namespace std;
class A {
public:
void callback(function<void()> cb) { cb(); }
};
class B {
public:
void ff_a(string&& input) {
void (B::*fp)(string &&) = &B::ff_a;
a.callback(bind(fp, this, move(input)));
}
int f_a(string&& input) {
cout << input << endl;
return 0;
}
A a;
};
int main() {
B().ff_a("Hello");
return 0;
}
error
error: no viable conversion from 'typename _Bind_helper<__is_socketlike<void (B::*&)(basic_string<char> &&)>::value, void (B::*&)(basic_string<char> &&), B *, basic_string<char>>::type' (aka '_Bind<void (B::*(B *, std::basic_string<char>))(std::basic_string<char> &&)>') to 'function<void ()>'
a.callback(bind(fp, this, move(input)));
^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/std_function.h:362:7: note: candidate constructor not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (B::*&)(basic_string<char> &&)>::value, void (B::*&)(basic_string<char> &&), B *, basic_string<char>>::type' (aka '_Bind<void (B::*(B *, std::basic_string<char>))(std::basic_string<char> &&)>') to 'std::nullptr_t' (aka 'nullptr_t') for 1st argument
function(nullptr_t) noexcept
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/std_function.h:373:7: note: candidate constructor not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (B::*&)(basic_string<char> &&)>::value, void (B::*&)(basic_string<char> &&), B *, basic_string<char>>::type' (aka '_Bind<void (B::*(B *, std::basic_string<char>))(std::basic_string<char> &&)>') to 'const std::function<void ()> &' for 1st argument
function(const function& __x)
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/std_function.h:391:7: note: candidate constructor not viable: no known conversion from 'typename _Bind_helper<__is_socketlike<void (B::*&)(basic_string<char> &&)>::value, void (B::*&)(basic_string<char> &&), B *, basic_string<char>>::type' (aka '_Bind<void (B::*(B *, std::basic_string<char>))(std::basic_string<char> &&)>') to 'std::function<void ()> &&' for 1st argument
function(function&& __x) noexcept
^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/11.2.0/../../../../include/c++/11.2.0/bits/std_function.h:414:2: note: candidate template ignored: requirement '_Callable<std::_Bind<void (B::*(B *, std::basic_string<char, std::char_traits<char>, std::allocator<char>>))(std::basic_string<char, std::char_traits<char>, std::allocator<char>> &&)>, std::__invoke_result<std::_Bind<void (B::*(B *, std::basic_string<char, std::char_traits<char>, std::allocator<char>>))(std::basic_string<char, std::char_traits<char>, std::allocator<char>> &&)> &>>::value' was not satisfied [with _Functor = std::_Bind<void (B::*(B *, std::basic_string<char>))(std::basic_string<char> &&)>, $1 = void]
function(_Functor __f)
^
test.cpp:18:35: note: passing argument to parameter 'cb' here
void callback(function<void()> cb) { cb(); }
^
1 error generated.
It seems that the bind function cannot bind the rvalue, How can I modify it if I use the bind function?