我需要使用std :: bind调用std :: string方法成员,我尝试了以下操作,但是它给出了一个错误:
#include <iostream>
#include <string>
#include <functional>
int main(int argc, char **argv) {
std::string test = "fdsa";
auto fn = std::bind(&std::string::append<std::string>, test, std::string("test"));
fn();
std::cout << test << '\n';
}
错误:
/home/alex/Tests/atom/main.cpp:9:6: error: no match for call to ‘(std::_Bind<std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>(std::basic_string<char>, std::basic_string<char>)>) ()’
fn();
^
In file included from /home/alex/Tests/atom/main.cpp:3:0:
/usr/include/c++/4.9/functional:1248:11: note: candidates are:
class _Bind<_Functor(_Bound_args...)>
^
/usr/include/c++/4.9/functional:1319:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) [with _Args = {_Args ...}; _Result = _Result; _Functor = std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>; _Bound_args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Args&&... __args)
^
/usr/include/c++/4.9/functional:1319:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1315:37: error: no match for call to ‘(std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>) (std::basic_string<char>&, std::basic_string<char>&)’
= decltype( std::declval<_Functor>()(
^
/usr/include/c++/4.9/functional:509:11: note: candidates are:
class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
^
/usr/include/c++/4.9/functional:555:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:555:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:560:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&&, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class&& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:560:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1315:37: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<std::basic_string<char>&, std::tuple<> >((* & std::declval<std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘std::basic_string<char>’) to type ‘std::basic_string<char>&&’
= decltype( std::declval<_Functor>()(
^
/usr/include/c++/4.9/functional:568:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class* __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:568:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1315:37: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<std::basic_string<char>&, std::tuple<> >((* & std::declval<std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘std::basic_string<char>’) to type ‘std::basic_string<char>*’
= decltype( std::declval<_Functor>()(
^
/usr/include/c++/4.9/functional:575:2: note: template<class _Tp, class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Tp&&, _Args&& ...) const [with _Tp = _Tp; _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Tp&& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:575:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:584:2: note: template<class _Tp, class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(std::reference_wrapper<_Tp>, _Args&& ...) const [with _Tp = _Tp; _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:584:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1315:37: note: ‘std::basic_string<char>’ is not derived from ‘std::reference_wrapper<_Tp>’
= decltype( std::declval<_Functor>()(
^
/usr/include/c++/4.9/functional:1333:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const [with _Args = {_Args ...}; _Result = _Result; _Functor = std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>; _Bound_args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Args&&... __args) const
^
/usr/include/c++/4.9/functional:1333:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1329:53: error: no match for call to ‘(const std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>) (const std::basic_string<char>&, const std::basic_string<char>&)’
typename add_const<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:509:11: note: candidates are:
class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
^
/usr/include/c++/4.9/functional:555:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:555:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1329:53: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<const std::basic_string<char>&, std::tuple<> >((* & std::declval<const std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘const std::basic_string<char>’) to type ‘std::basic_string<char>&’
typename add_const<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:560:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&&, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class&& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:560:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1329:53: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<const std::basic_string<char>&, std::tuple<> >((* & std::declval<const std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘const std::basic_string<char>’) to type ‘std::basic_string<char>&&’
typename add_const<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:568:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class* __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:568:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1329:53: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<const std::basic_string<char>&, std::tuple<> >((* & std::declval<const std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘const std::basic_string<char>’) to type ‘std::basic_string<char>*’
typename add_const<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:575:2: note: template<class _Tp, class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Tp&&, _Args&& ...) const [with _Tp = _Tp; _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Tp&& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:575:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:584:2: note: template<class _Tp, class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(std::reference_wrapper<_Tp>, _Args&& ...) const [with _Tp = _Tp; _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:584:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1329:53: note: ‘std::basic_string<char>’ is not derived from ‘std::reference_wrapper<_Tp>’
typename add_const<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:1347:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>; _Bound_args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Args&&... __args) volatile
^
/usr/include/c++/4.9/functional:1347:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1343:70: error: no match for call to ‘(volatile std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>) (volatile std::basic_string<char>&, volatile std::basic_string<char>&)’
typename add_volatile<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:509:11: note: candidates are:
class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
^
/usr/include/c++/4.9/functional:555:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:555:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1343:70: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<volatile std::basic_string<char>&, std::tuple<> >((* & std::declval<volatile std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘volatile std::basic_string<char>’) to type ‘std::basic_string<char>&’
typename add_volatile<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:560:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&&, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class&& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:560:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1343:70: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<volatile std::basic_string<char>&, std::tuple<> >((* & std::declval<volatile std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘volatile std::basic_string<char>’) to type ‘std::basic_string<char>&&’
typename add_volatile<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:568:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class* __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:568:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1343:70: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<volatile std::basic_string<char>&, std::tuple<> >((* & std::declval<volatile std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘volatile std::basic_string<char>’) to type ‘std::basic_string<char>*’
typename add_volatile<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:575:2: note: template<class _Tp, class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Tp&&, _Args&& ...) const [with _Tp = _Tp; _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Tp&& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:575:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:584:2: note: template<class _Tp, class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(std::reference_wrapper<_Tp>, _Args&& ...) const [with _Tp = _Tp; _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:584:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1343:70: note: ‘std::basic_string<char>’ is not derived from ‘std::reference_wrapper<_Tp>’
typename add_volatile<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:1361:2: note: template<class ... _Args, class _Result> _Result std::_Bind<_Functor(_Bound_args ...)>::operator()(_Args&& ...) const volatile [with _Args = {_Args ...}; _Result = _Result; _Functor = std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>; _Bound_args = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Args&&... __args) const volatile
^
/usr/include/c++/4.9/functional:1361:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1357:64: error: no match for call to ‘(const volatile std::_Mem_fn<std::basic_string<char>& (std::basic_string<char>::*)(std::basic_string<char>, std::basic_string<char>)>) (const volatile std::basic_string<char>&, const volatile std::basic_string<char>&)’
typename add_cv<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:509:11: note: candidates are:
class _Mem_fn<_Res (_Class::*)(_ArgTypes...)>
^
/usr/include/c++/4.9/functional:555:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:555:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1357:64: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<const volatile std::basic_string<char>&, std::tuple<> >((* & std::declval<const volatile std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘const volatile std::basic_string<char>’) to type ‘std::basic_string<char>&’
typename add_cv<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:560:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&&, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class&& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:560:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1357:64: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<const volatile std::basic_string<char>&, std::tuple<> >((* & std::declval<const volatile std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘const volatile std::basic_string<char>’) to type ‘std::basic_string<char>&&’
typename add_cv<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:568:2: note: template<class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _Args&& ...) const [with _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Class* __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:568:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1357:64: note: cannot convert ‘std::_Mu<std::basic_string<char>, false, false>().std::_Mu<_Arg, false, false>::operator()<const volatile std::basic_string<char>&, std::tuple<> >((* & std::declval<const volatile std::basic_string<char>&>()), (* & std::declval<std::tuple<>&>()))’ (type ‘const volatile std::basic_string<char>’) to type ‘std::basic_string<char>*’
typename add_cv<_Functor>::type>::type>()(
^
/usr/include/c++/4.9/functional:575:2: note: template<class _Tp, class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Tp&&, _Args&& ...) const [with _Tp = _Tp; _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(_Tp&& __object, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:575:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:584:2: note: template<class _Tp, class ... _Args, class _Req> _Res std::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(std::reference_wrapper<_Tp>, _Args&& ...) const [with _Tp = _Tp; _Args = {_Args ...}; _Req = _Req; _Res = std::basic_string<char>&; _Class = std::basic_string<char>; _ArgTypes = {std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >}]
operator()(reference_wrapper<_Tp> __ref, _Args&&... __args) const
^
/usr/include/c++/4.9/functional:584:2: note: template argument deduction/substitution failed:
/usr/include/c++/4.9/functional:1357:64: note: ‘std::basic_string<char>’ is not derived from ‘std::reference_wrapper<_Tp>’
typename add_cv<_Functor>::type>::type>()(
^
In file included from /usr/include/c++/4.9/string:52:0,
from /usr/include/c++/4.9/bits/locale_classes.h:40,
from /usr/include/c++/4.9/bits/ios_base.h:41,
from /usr/include/c++/4.9/ios:42,
from /usr/include/c++/4.9/ostream:38,
from /usr/include/c++/4.9/iostream:39,
from /home/alex/Tests/atom/main.cpp:1:
/usr/include/c++/4.9/bits/basic_string.h: In instantiation of ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std::__false_type) [with _InIterator = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’:
/usr/include/c++/4.9/bits/basic_string.h:1764:58: required from ‘static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
/usr/include/c++/4.9/bits/basic_string.tcc:229:49: required from ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
/usr/include/c++/4.9/bits/basic_string.tcc:658:35: required from ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::_M_replace_dispatch(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _InputIterator = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; typename _Alloc::rebind<_CharT>::other::pointer = char*]’
/usr/include/c++/4.9/bits/basic_string.h:1648:66: required from ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::replace(std::basic_string<_CharT, _Traits, _Alloc>::iterator, std::basic_string<_CharT, _Traits, _Alloc>::iterator, _InputIterator, _InputIterator) [with _InputIterator = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc>::iterator = __gnu_cxx::__normal_iterator<char*, std::basic_string<char> >; typename _Alloc::rebind<_CharT>::other::pointer = char*]’
/usr/include/c++/4.9/bits/basic_string.h:1062:69: required from ‘std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::append(_InputIterator, _InputIterator) [with _InputIterator = std::basic_string<char>; _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
/home/alex/Tests/atom/main.cpp:8:83: required from here
/usr/include/c++/4.9/bits/basic_string.h:1742:76: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<std::basic_string<char> >’
typedef typename iterator_traits<_InIterator>::iterator_category _Tag;
我想知道,我怎样才能使用bind调用std :: string中的方法。
答案 0 :(得分:4)
std::string::append
有multiple overloads:您需要告诉编译器您想要哪一个,请使用static_cast
:
#include <iostream>
#include <string>
#include <functional>
int main(int argc, char **argv) {
std::string test = "fdsa";
auto fn = std::bind(static_cast<std::string&(std::string::*)(const std::string&)>(&std::string::append), std::ref(test), std::string("test"));
fn();
std::cout << test << '\n';
}
答案 1 :(得分:2)
由于问题标记为C ++ 11,您可以改为使用lambda:
std::string test = "fdsa";
auto fn = [&](){
return test.append("test");
};
std::cout << fn() << std::endl;