std::bind 不适用于字符串向量

std::bind doesn't work with string vector

本文关键字:字符串 向量 适用于 不适用 bind std      更新时间:2023-10-16

有没有办法让这段代码编译?

1  #include <functional>
2  #include <vector>
3  #include <iostream>
4  using namespace std;
5  void f1(int x, int y){}
6  void f2(int x, vector<string> v) {}
7  int main ()
8  {
9      f2(2, {{"hello", "it's", "me"}});
10     auto g1 = bind(f1, placeholders::_1, 3);
11     auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
12     return 0;
13 }

G1 工作正常,G2 给出一些复杂的编译错误

编辑:通过流行的请求,我给你错误:

In file included from test.cpp:1:0:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_check_arity<void (*)(int, std::vector<std::__cxx11::basic_string<char> >)>’:
/usr/include/c++/5/functional:1439:12:   required from ‘struct std::_Bind_helper<false, void (&)(int, std::vector<std::__cxx11::basic_string<char> >)>’
/usr/include/c++/5/functional:1462: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 (&)(int, std::vector<std::__cxx11::basic_string<char> >); _BoundArgs = {}]’
test.cpp:11:67:   required from here
/usr/include/c++/5/functional:1410:7: error: static assertion failed: Wrong number of arguments for function
       static_assert(sizeof...(_BoundArgs) == sizeof...(_Args),
       ^
test.cpp: In function ‘int main()’:
test.cpp:11:67: error: no matching function for call to ‘bind(void (&)(int, std::vector<std::__cxx11::basic_string<char> >), const std::_Placeholder<1>&, <brace-enclosed initializer list>)’
     auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
                                                                   ^
In file included from test.cpp:1:0:
/usr/include/c++/5/functional:1462:5: note: candidate: typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (&)(int, std::vector<std::__cxx11::basic_string<char> >); _BoundArgs = {}; typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type = std::_Bind<void (*())(int, std::vector<std::__cxx11::basic_string<char> >)>]
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/usr/include/c++/5/functional:1462:5: note:   candidate expects 1 argument, 3 provided
/usr/include/c++/5/functional:1490:5: note: candidate: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/usr/include/c++/5/functional:1490:5: note:   template argument deduction/substitution failed:
test.cpp:11:67: note:   couldn't deduce template parameter ‘_Result’
     auto g2 = bind(f2, placeholders::_1, {{"hello", "it's", "me"}});
                                                                   ^
#include <functional>
#include <vector>
#include <iostream>
using namespace std;
void f1(int x, int y){}
void f2(int x, vector<string> v) {}
int main ()
{
    f2(2, {{"hello", "it's", "me"}});
    auto g1 = bind(f1, placeholders::_1, 3); 
    auto g2 = bind(f2, placeholders::_1, vector<string>{{"hello", "it's", "me"}});
    return 0;
}