问题
I have a member function as follows:
class XYZ{
public:
float function(float x);
private:
float m_DensityMin;
float m_DensityMax;
};
Now, I'm trying to transform a std::vector<float> foo
using the std::transform
STL algorithm by passing the member function function
, and storing the resulting values in a vector bar
.
If I use the function as a global function, with random data that should denote the member variables of the class, it works fine.
However, as the function requires the use of member variables m_DensityMin
and m_DensityMax
of the class, I need to use it as a member function. This is what I've tried:
std::transform(foo.begin(), foo.end(), bar.begin(), &XYZ::function);
but I end up with the error in VS2010:
error C2065: term does not evaluate to a function taking 1 arguments
As far as I can tell I'm passing only 1 argument. Any pointers? Here's a similar question, I've tried with std::mem_fun, as std::mem_fn is not available to me, but to no avail.
回答1:
std::mem_fun itself only povides a wrapper for a member function, so that one will be invoked in the context of the first argument passed to its operator()
. Having said that, you need to bind the proper instance of XYZ
prior to passing the function object to the std::transform algorithm:
XYZ xyz;
std::transform(foo.begin(), foo.end(),
std::back_inserter(bar),
std::bind1st(std::mem_fun(&XYZ::function), &xyz));
// ~~~~~~^ ~~~~~~^ ~~~^
DEMO
回答2:
Unless the function is static
, it needs an instance of the class to call the function off of. Because non-static functions should depend on the internal state of that class. If the function is independent of the class's state, it should probably be static.
If you had to leave your class as written, you could get around this by default constructing an XYZ
just for the purposes of having an instance to call the function from.
std::transform(foo.begin(),
foo.end(),
std::back_inserter(bar),
[](float a){ return XYZ{}.function(a); });
But this feels hacky, I would prefer that the function be static
if it isn't dependent on the state of the XYZ
instance, and only depends on the input float
.
回答3:
lambda version
std::vector<float> foo;
std::vector<float> bar;
foo.push_back(1.0f);
foo.push_back(2.0f);
XYZ xyz;
std::transform(foo.begin(), foo.end(), std::back_inserter(bar),
[&xyz](float& a){ return xyz.function(a); });
for ( auto & x : bar)
std::cout<<x<<"\n";
来源:https://stackoverflow.com/questions/30354718/passing-a-c-member-function-pointer-to-an-stl-algorithm