【发布时间】:2017-07-26 15:18:47
【问题描述】:
我想将mem_fn 参数传递给bind,但编译器似乎不允许这样做。
例如,这可以正常工作:
accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, bind(&foo::r, placeholders::_2)));
但是当我尝试使用 mem_fn 仿函数时,我得到了一个错误页面:
accumulate(cbegin(foos), cend(foos), 0, bind(plus<int>(), placeholders::_1, mem_fn(&foo::r)));
/usr/include/c++/6/bits/stl_numeric.h: 在'_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) 的实例化中 [with _InputIterator = __gnu_cxx::__normal_iterator >; _Tp = int; _BinaryOperation = std::_Bind(std::_Placeholder, std::_Mem_fn)>]’:
prog.cpp:20:102: 从这里需要
/usr/include/c++/6/bits/stl_numeric.h:154:22:错误:不匹配调用 '(std::_Bind(std::_Placeholder, std::_Mem_fn)>) (int& , foo* const&)'
【问题讨论】:
-
你是我看到的第一个在不知情的情况下使用
bind对嵌套绑定表达式的特殊处理的人。 -
mem_fn 已弃用,请勿使用。
-
@n.m.它是?我知道它里面的 typedefs 将在 C++17 中,但我没有看到任何关于
mem_fn本身被弃用的信息。 -
@NathanOliver 抱歉把它和 mem_fun 搞混了。
-
@n.m.啊。是的,他们没有让这最容易保持直线。
标签: c++ c++11 bind functor mem-fun