std :: mem_fun vs std :: mem_fn [英] std::mem_fun vs std::mem_fn
问题描述
std :: mem_fun 和 std :: mem_fn 之间有什么区别?为什么命名如此混乱?
What is the difference between std::mem_fun and std::mem_fn? Why is the naming so confusing?
Boost的文档说,在大多数情况下,std :: mem_fn可以替换std :: mem_fun。那么在什么情况下你仍然会使用std :: mem_fun?
Boost's documentation says that std::mem_fn can replace std::mem_fun in most cases. So in what situation would you still use std::mem_fun?
推荐答案
std :: mem_fun
已被弃用。
std :: mem_fn
可以做它做的一切,它做它更方便。两者之间的关系与 std :: bind1st
/ std :: bind2nd
和C ++ 11 std :: bind
。在 std ::后开发并掌握
和 std :: mem_fn
和 std :: bind
bind1st std :: mem_fun
被写入C ++ 98标准。所以这意味着我们必须等到C ++ 11才能用更好的替代方案来替换旧的东西。
std::mem_fun
is deprecated. std::mem_fn
can do everything it does, and it does it more conveniently. The relation between the two is the same as the relation between std::bind1st
/std::bind2nd
and the C++11 std::bind
. Both std::mem_fn
and std::bind
were developed and mastered after std::bind1st
and std::mem_fun
were made into the C++98 Standard. So that means we had to wait until C++11 to properly replace the old stuff with the superior alternatives.
例如, std :: mem_fun
只能处理带有一个或没有参数的成员函数。 std :: mem_fn
是可变的,可以处理拥有任意数量参数的成员。
For instance, std::mem_fun
can only deal with member functions that take one or no argument. std::mem_fn
is variadic and can deal with members that take any number of arguments.
在 std :: mem_fun
和 std :: mem_fun_ref
之间选择取决于是否要处理指针或引用类对象(分别)。 std :: mem_fn
单独可以处理,甚至提供对智能指针的支持。
You also need to pick between std::mem_fun
and std::mem_fun_ref
depending on whether you want to deal with pointers or references for the class object (respectively). std::mem_fn
alone can deal with either, and even provides support for smart pointers.
code> boost :: mem_fn 解释何时使用 std :: mem_fun
,简单地说,当你需要使用代码期望 std :: mem_fun
,或者期望可适应的函子(这是来自C ++ 03的过时的概念)。对于这些情况,你不能插入 std :: mem_fn
,所以你有它:你会使用 std :: mem_fun
The documentation of boost::mem_fn
explains when to use std::mem_fun
, and put simply that's when you need to operate with code that expects std::mem_fun
, or that expects adaptable functors (which is an obsolete notion* from C++03). For those cases you wouldn't be able to plug in std::mem_fn
either, so there you have it: you would use std::mem_fun
for legacy.
*:我的意思是新代码不应该依赖于C ++ 03协议 result_type
成员类型(使用像 std :: result_of
这样的新特性更常见) std :: bind
/ std :: mem_fn
实际上提供那些成员,如果他们将存在于等效的C ++ 03代码。我离开它来确定你是否应该更新依赖于适应性函数与 std :: mem_fn
依靠此行为的旧代码。
*: I mean by that that new code shouldn't rely on the C++03 protocol of having e.g. result_type
member types (it's more customary to use the new traits like std::result_of
) -- the new facilities like std::bind
/std::mem_fn
do in fact provide those members if they would have been present in equivalent C++03 code. I leave it to you to figure out whether you should update old code that relies on adaptable functors with std::mem_fn
by relying on this behaviour.
这篇关于std :: mem_fun vs std :: mem_fn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!