2

我不能使用 C++11。

通常在将 std::for_each 与成员函数一起使用时,提供的数据结构会给出调用绑定成员函数的对象。但是,我想知道是否可以提供我自己的在整个循环中保持不变的对象,并将数据结构用于该成员函数的参数。

这是我想做的一个例子。

std::string GenerateKey(const std::vector<std::string>& parameters)
{
    std::string key;
    key.reserve(std::accumulate(parameters.begin(), parameters.end(), 0, std::mem_fun_ref(&std::string::length)));
    std::for_each(parameters.begin(), parameters.end(), std::bind1st(std::mem_fun_ref(&std::string::append), key));
    return key;
}

目的是对于参数中的每个字符串,append 将在键上调用,并从参数中传递一个字符串。

为什么这个 std::for_each 不起作用,如何或可以使它起作用?

我知道有其他方法可以连接字符串。我想更好地理解 C++ 函数式编程。

谢谢。

这是来自 VS2013 的错误消息,我怀疑它会有所帮助..

generator.cpp(28): error C2784: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Result'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Ty'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg) const)' : could not deduce template argument for '_Arg'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(910) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2784: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void) const)' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void) const)' : could not deduce template argument for '_Result'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::const_mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void) const)' : could not deduce template argument for '_Ty'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(901) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2784: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg))' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2914: 'std::mem_fun_ref' : cannot deduce template argument as function argument is ambiguous
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg))' : could not deduce template argument for '_Result'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg))' : could not deduce template argument for '_Ty'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::mem_fun1_ref_t<_Result,_Ty,_Arg> std::mem_fun_ref(_Result (__thiscall _Ty::* )(_Arg))' : could not deduce template argument for '_Arg'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(893) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2784: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void))' : could not deduce template argument for 'overloaded function type' from 'overloaded function type'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void))' : could not deduce template argument for '_Result'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2783: 'std::mem_fun_ref_t<_Result,_Ty> std::mem_fun_ref(_Result (__thiscall _Ty::* )(void))' : could not deduce template argument for '_Ty'
          c:\program files\microsoft visual studio 12.0\vc\include\xfunctional(884) : see declaration of 'std::mem_fun_ref'
generator.cpp(28): error C2780: '_Fn1 std::for_each(_InIt,_InIt,_Fn1)' : expects 3 arguments - 2 provided
          c:\program files\microsoft visual studio 12.0\vc\include\algorithm(29) : see declaration of 'std::for_each'
4

2 回答 2

1

std::string::append已重载,编译器无法知道您的意思是哪个函数。

所以,std::string::append实际上是指一组功能。

于 2013-08-15T12:31:54.303 回答
0

这个错误是因为有一些重载版本的函数 std::string::append,你可以使用 cast 来告诉编译器需要使用什么确切的函数:

std::for_each(parameters.begin(), parameters.end(), std::bind1st(std::mem_fun_ref(static_cast<std::string& (std::string::*)(const std::string&)>(&std::string::append)), key));

编辑:但在这种情况下它不会起作用,因为 bind1st 不能与将引用作为参数的函数一起使用。请参阅:将 bind1st 用于通过引用获取参数的方法

于 2013-08-15T12:32:09.803 回答