C++ 将std::bind应用于带参数的函数时出错<;boost::asio::ip::tcp::socket>;?

C++ 将std::bind应用于带参数的函数时出错<;boost::asio::ip::tcp::socket>;?,c++,boost,c++14,C++,Boost,C++14,有一个函数接受类型为boost::asio::ip::tcp::socket的参数。但是我没有将std::bind应用到它。我已经检查了std::\u Bind\u helper中的模板参数,但没有发现问题。我试过gcc 8.1.0和clang 6.0.0,它们给出了相同的错误 #include <functional> #include <boost/asio.hpp> using namespace std; namespace asio = boost::asio;

有一个函数接受类型为
boost::asio::ip::tcp::socket
的参数。但是我没有将std::bind应用到它。我已经检查了std::\u Bind\u helper中的模板参数,但没有发现问题。我试过gcc 8.1.0和clang 6.0.0,它们给出了相同的错误

#include <functional>
#include <boost/asio.hpp>
using namespace std;
namespace asio = boost::asio;
using tcp = asio::ip::tcp;

int c;

void good_f(int) {c=1;}
void good() {
    using typeA = int;

    auto helper = std::bind(&good_f, typeA(1));
    helper();
    // OK
}

void fixed_f(tcp::socket &) {c=1;}
void fixed() {
    using type1 = tcp::socket;
    asio::io_context ioc;
    tcp::socket sock(ioc);

    auto helper = std::bind(&fixed_f, std::ref(sock));
    helper();
    // OK
}

void fail_f(tcp::socket &&) {c=1;}
void fail() {
    using type1 = tcp::socket;
    asio::io_context ioc;
    tcp::socket sock(ioc);

    // fail_f(std::move(sock));
    // OK

    auto helper = std::bind(&fail_f, std::move(sock));
    helper();
    // a.cc:34:5: error: no matching function for call to object of type 'std::_Bind<void (*(boost::asio::basic_stream_socket<boost::asio::ip::tcp>))(boost::asio::basic_stream_socket<boost::asio::ip::tcp> &&)>'
    //    helper();
    //    ^~~~~~
}
#包括
#包括
使用名称空间std;
名称空间asio=boost::asio;
使用tcp=asio::ip::tcp;
INTC;
void good_f(int){c=1;}
无效货物{
使用typeA=int;
auto-helper=std::bind(&good_f,typeA(1));
助手();
//嗯
}
void修复了_f(tcp::socket&){c=1;}
无效固定(){
使用type1=tcp::socket;
asio::io_上下文ioc;
tcp::socketsock(ioc);
auto-helper=std::bind(&fixed_f,std::ref(sock));
助手();
//嗯
}
void fail_f(tcp::socket&){c=1;}
无效失败(){
使用type1=tcp::socket;
asio::io_上下文ioc;
tcp::socketsock(ioc);
//失败(标准::移动(袜子));
//嗯
自动助手=std::bind(&fail_f,std::move(sock));
助手();
//a.cc:34:5:错误:对“std::\u Bind”类型的对象的调用没有匹配函数
//助手();
//    ^~~~~~
}
编译日志和错误:

recolic@RECOLICPC ~/tmp> clang++ --version
clang version 6.0.0 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
recolic@RECOLICPC ~/tmp> clang++ -c a.cc
a.cc:39:5: error: no matching function for call to object of type 'std::_Bind<void (*(boost::asio::basic_stream_socket<boost::asio::ip::tcp>))(boost::asio::basic_stream_socket<boost::asio::ip::tcp> &&)>'
    helper();
    ^~~~~~
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../include/c++/8.1.0/functional:480:2: note: candidate template ignored: substitution failure [with _Args = <>]: no type named 'type' in 'std::result_of<void
      (*&(boost::asio::basic_stream_socket<boost::asio::ip::tcp> &))(boost::asio::basic_stream_socket<boost::asio::ip::tcp> &&)>'
        operator()(_Args&&... __args)
        ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../include/c++/8.1.0/functional:491:2: note: candidate template ignored: substitution failure [with _Args = <>]: no type named 'type' in 'std::result_of<void (*const &(const
      boost::asio::basic_stream_socket<boost::asio::ip::tcp> &))(boost::asio::basic_stream_socket<boost::asio::ip::tcp> &&)>'
        operator()(_Args&&... __args) const
        ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../include/c++/8.1.0/functional:509:2: note: candidate template ignored: substitution failure [with _Args = <>]: no type named 'type' in 'std::result_of<void (*volatile &(volatile
      boost::asio::basic_stream_socket<boost::asio::ip::tcp> &))(boost::asio::basic_stream_socket<boost::asio::ip::tcp> &&)>'
        operator()(_Args&&... __args) volatile
        ^
/usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../include/c++/8.1.0/functional:521:2: note: candidate template ignored: substitution failure [with _Args = <>]: no type named 'type' in 'std::result_of<void (*const volatile &(const volatile
      boost::asio::basic_stream_socket<boost::asio::ip::tcp> &))(boost::asio::basic_stream_socket<boost::asio::ip::tcp> &&)>'
        operator()(_Args&&... __args) const volatile
        ^
1 error generated.
recolic@RECOLICPC~/tmp>clang++--版本
clang版本6.0.0(标签/发行版600/最终版)
目标:x86_64-pc-linux-gnu
线程模型:posix
已安装dir:/usr/bin
recolic@RECOLICPC~/tmp>clang++-ca.cc
a、 cc:39:5:错误:对“std::\u Bind”类型的对象的调用没有匹配函数
助手();
^~~~~~
/usr/bin/./lib64/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../../../../../include/c++/8.1.0/functional:480:2:注意:忽略候选模板:替换失败[with _Args=]:在“std::result_of”中没有名为“type”的类型
运算符()
^
/usr/bin/./lib64/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../../../../../include/c++/8.1.0/functional:491:2:注意:忽略候选模板:替换失败[with _Args=]:在“std::result_of”中没有名为“type”的类型
运算符()
^
/usr/bin/./lib64/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../../../../include/c++/8.1.0/functional:509:2:注意:忽略候选模板:替换失败[带_Args=]:在“std::result_of”中没有名为“type”的类型
运算符()
^
/usr/bin/./lib64/gcc/x86_64-pc-linux-gnu/8.1.0/../../../../../../../../include/c++/8.1.0/functional:521:2:注意:忽略候选模板:替换失败[with _Args=]:在“std::result_of”中没有名为“type”的类型
运算符()
^
生成1个错误。
我不确定是什么导致了这个错误。你能给我一些建议吗?

void fail\u f\u 1(tcp::socket);
std::bind(&fail_f_1,tcp::socket(io_上下文));
此解决方案失败,因为
boost::asio::ip::tcp::socket
不可复制

void fail_f_2(tcp::socket&&);
std::bind(&fail_f_2,std::move(tcp::socket(io_上下文)));
此解决方案失败,因为
std::bind
将绑定参数作为左值而不是右值传递
fixed\u f
是一个很好的解决方案


如果必须将右值引用作为绑定参数传递,这将有所帮助。

Write
auto f=std::bind(某物)@路人我已经试过了,但是没有用。返回类型正是我在这里写的。不可复制构造/分配,因此
fail\f
需要通过引用而不是通过值接受其参数。另外,按照@PasserBy的建议使用
auto
。绑定参数始终作为左值传递。@RecolicKeghart请从标题中删除“已解决”,然后添加解决方案作为答案。Stackoverflow不是一个论坛。