Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么可以';我不能在bind中使用mem_fn函子吗?_C++_C++11_Bind_Functor_Mem Fun - Fatal编程技术网

C++ 为什么可以';我不能在bind中使用mem_fn函子吗?

C++ 为什么可以';我不能在bind中使用mem_fn函子吗?,c++,c++11,bind,functor,mem-fun,C++,C++11,Bind,Functor,Mem Fun,我想将memfn参数传递给bind,但编译器似乎不允许这样做 例如,这很好: accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, bind(&foo::r, placeholders::_2))); /usr/include/c++/6/bits/stl_numeric.h:在“_tpstd::acculate”(_inputierator,_inputierator,_Tp

我想将
memfn
参数传递给
bind
,但编译器似乎不允许这样做

例如,这很好:

accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, bind(&foo::r, placeholders::_2)));
/usr/include/c++/6/bits/stl_numeric.h:在“_tpstd::acculate”(_inputierator,_inputierator,_Tp,_Tp,_BinaryOperation)[带_inputierator=u gnu cxx:u normal_iterator>_Tp=int;_Bind:_Bind(std:_占位符,std:_memfn)>]”的实例化中:
程序cpp:20:102:此处需要
/usr/include/c++/6/bits/stl_numeric.h:154:22:错误:调用(std::_Bind(std:_占位符,std:_Mem_fn)>)时不匹配(int&,foo*const&)


显然,第二个例子没有提到占位符::_2。当
accumulate
调用带有两个参数的函子时,第二个参数将被忽略,并且您的代码尝试将
int
mem\u fn
返回的内部类的实例相加

我建议您放弃所有这些
bind
游戏,使用lambda:

accumulate(cbegin(foos), cend(foos), 0, 
    [](int val, foo* f) { return val + f->r(); });
accumulate(cbegin(foos), cend(foos), 0, [](const int augend, const auto& addend) { return augend + addend.r(); } )

这里发生的事情要清楚得多。

显然,第二个例子没有提到占位符::_2。当
accumulate
调用带有两个参数的函子时,第二个参数将被忽略,并且您的代码尝试将
int
mem\u fn
返回的内部类的实例相加

我建议您放弃所有这些
bind
游戏,使用lambda:

accumulate(cbegin(foos), cend(foos), 0, 
    [](int val, foo* f) { return val + f->r(); });
accumulate(cbegin(foos), cend(foos), 0, [](const int augend, const auto& addend) { return augend + addend.r(); } )

这里发生的事情要清楚得多。

要理解这一点,请考虑一下,如果您只是将一个文本传递给
bind
的第三个参数,这意味着什么。例如,如果您已执行以下操作:

accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, 13))
无法编译,因为它试图将
mem\u fn(&foo::r)
的结果作为
plus
的加数传递。由于无法将其转换为
int
plus
无法接受。但是,即使可以将其转换为
int
,这也不是您要寻找的,您希望使用第二个参数并对其调用
foo::r
,将结果传递给
plus
。因此我们知道我们需要看到,
占位符::_2
在语句中的某处使用,传递调用它的
r
方法的第二个参数


我们需要绑定
占位符::_2
以绑定到一个函子,该函子将对其参数调用
r
方法。绑定当然需要
bind
,但实际上是这样

也就是说,工作代码中的
bind(&foo::r,占位符::\u 2)
语句在非嵌套形式中没有任何意义;那个函子甚至不带两个参数!实际上有,以便它们可以共享外部的
绑定
占位符,以免无法将绑定参数传递给嵌套表达式:

如果存储的参数arg的类型为
T
,其
std::is_bind_expression::value==true
(例如,另一个
bind
表达式直接传递到对
bind
的初始调用中),然后
bind
执行函数组合:不传递bind子表达式将返回的函数对象,而是急切地调用子表达式,并将其返回值传递给外部可调用对象。如果
bind
子表达式有任何占位符参数,则它们将与外部
bind
共享


在这个表达式中使用
mem\u fn
的唯一方法是将它的结果传递给
bind
,以便传递
占位符:::\u 2
bind(mem\u fn(&foo::r),占位符::\u 2)
这是有效的,但如果简单的
bind(&foo::r,占位符::\u 2)
就足够了,这是一个不必要的步骤。因此,生成此函子的最佳方法是使用您提供的语句:

accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, bind(&foo::r, placeholders::_2)))

要理解这一点,请考虑一下,如果您只是将一个文本传递给
bind
的第三个参数,这意味着什么。例如,如果您已执行以下操作:

accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, 13))
无法编译,因为它试图将
mem\u fn(&foo::r)
的结果作为
plus
的加数传递。由于无法将其转换为
int
plus
无法接受。但是,即使可以将其转换为
int
,这也不是您要寻找的,您希望使用第二个参数并对其调用
foo::r
,将结果传递给
plus
。因此我们知道我们需要看到,
占位符::_2
在语句中的某处使用,传递调用它的
r
方法的第二个参数


我们需要绑定
占位符::_2
以绑定到一个函子,该函子将对其参数调用
r
方法。绑定当然需要
bind
,但实际上是这样

也就是说,工作代码中的
bind(&foo::r,占位符::\u 2)
语句在非嵌套形式中没有任何意义;那个函子甚至不带两个参数!实际上有,以便它们可以共享外部的
绑定
占位符,以免无法将绑定参数传递给嵌套表达式:

如果存储的参数arg的类型为
T
,其
std::is_bind_expression::value==true
(例如,另一个
bind
表达式直接传递到对
bind
的初始调用中),然后
bind
执行函数组合:不传递bind子表达式将返回的函数对象,而是急切地调用子表达式,并将其返回值传递给外部可调用对象。如果
bind
子表达式有任何占位符参数,则它们将与外部
bind
共享


在这个表达式中使用
mem\u fn
的唯一方法是将它的结果传递给
bind
,以便传递
占位符::\u 2
bind(mem\u fn(&foo::r),占位符::\u 2)
这个w