问题描述
这是跟进的问题
这是当前代码.
#include <vector>
#include <algorithm>
#include <functional>
struct Int
{
Int(int _x = 0) : x(_x) {}
int GetInt() const { return x; }
int x;
};
struct IntWrapper
{
IntWrapper(int _x = 0) : test(_x) {}
int GetWrappedInt() const { return test.GetInt(); }
Int test;
};
template<class ContainerT, class Mem> constexpr auto maxElem(const ContainerT& _container, Mem _Pm)
{
auto memFn = std::mem_fn(_Pm);
return memFn(std::max_element(_container.cbegin(), _container.cend(), [&](auto _rhs, auto _lhs) { return memFn(_rhs) < memFn(_lhs); }));
}
int main()
{
{
std::vector<Int> vec;
for (int i = 0; i < 10; ++i)
{
vec.push_back(i * 11 % 7); // some random values
}
int m = maxElem(vec, &Int::GetInt);
int n = maxElem(vec, &Int::x);
}
{
std::vector<IntWrapper> vec;
for (int i = 0; i < 10; ++i)
{
vec.push_back(i * 7 % 11); // some random values
}
int m = maxElem(vec, &IntWrapper::GetWrappedInt);
//int o = maxElem(vec, ???) // what if GetWrappedInt didn't exist?
}
return 0;
}
最初的问题是关于通过IntWrapper
对象检索Int
结构的x
值.我之所以使用mem_fn
是因为它似乎无法区分返回int
和int
成员变量的函数(在以下几行中看到:
The original question was about retrieving the x
value of the Int
struct through anIntWrapper
object. I used mem_fn
for this because it doesn't seem to distinguish between a function returning an int
and an int
member variable (Seen in these lines:
int m = maxElem(vec, &Int::GetInt);
int n = maxElem(vec, &Int::x);
IntWrapper
对象的解决方案是添加.test
The solution for IntWrapper
objects was to add .test
auto y = std::mem_fn(&Int::GetInt);
auto b = y(wrapper.test);
拨打电话.但是,在maxElem
函数中,我无法执行此操作.
to the call. However, in the maxElem
function, I cannot do this.
我想知道是否有一种方法可以这样构造调用:mem_fn
从IntWrapper
对象直接转到int x
变量(不使用helper函数,并假定所有成员都是公开).
I'm wondering if there is a way to formulate the call in such a way that the mem_fn
goes from the IntWrapper
object directly to the int x
variable (Without the helper function and assuming that all members are public).
//int o = maxElem(vec, ???) // what if GetWrappedInt didn't exist?
最初的方法是auto y = std::mem_fn(&IntWrapper::test.GetInt); // ERROR
,它当然不会编译,但是可以说明问题.
The original approach was auto y = std::mem_fn(&IntWrapper::test.GetInt); // ERROR
, which of course does not compile, but shows the idea.
提前谢谢!
推荐答案
您不能将std::mem_fn
与指向成员的指针(例如,指向成员的成员的指针)不同.因此,您必须使用它.在您的特定情况下,您可以使用
You cannot use std::mem_fn
with something different than a pointer to member (such as a pointer to member of member). So, you must use that. In your particular case, you can achieve that with
std::vector<IntWrapper> vec;
for (int i = 0; i < 10; ++i)
{
vec.push_back(i * 11 % 7); // some random values
}
auto m = maxElem(vec, &IntWrapper::GetWrappedInt);
但是,我强烈建议您尽可能使用 lambda表达式.应该将std::mem_fn
视为已弃用,因为AFAIK并不具有至少通过其他方式(例如lambda)无法达到的目的.
However, I strongly advise you to use lambda expressions whenever possible. std::mem_fn
should be considered as if deprecated, since, AFAIK, it serves no purpose that cannot be achieved at least as well by other means, i.e. a lambda.
这篇关于成员的mem_fn至mem_fn的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!