I’m looking at boost::lambda as a way to to make a generic algorithm that can work with any “getter” method of any class.
The algorithm is used to detect duplicate values of a property, and I would like for it to work for any property of any class.
In C#, I would do something like this:
class Dummy
{
public String GetId() ...
public String GetName() ...
}
IEnumerable<String> FindNonUniqueValues<ClassT>
(Func<ClassT,String> propertyGetter) { ... }
Example use of the method:
var duplicateIds = FindNonUniqueValues<Dummy>(d => d.GetId());
var duplicateNames = FindNonUniqueValues<Dummy>(d => d.GetName());
I can get the for “any class” part to work, using either interfaces or template methods, but have not found yet how to make the “for any method” part work.
Is there a way to do something similar to the “d => d.GetId()” lambda in C++ (either with or without Boost)?
Alternative, more C++ian solutions to make the algorithm generic are welcome too.
I’m using C++/CLI with VS2008, so I can’t use C++0x lambdas.
Assuming, I understand what you’re looking for, you can use
boost::bind:Actually, you just need
boost::mem_fnor evenstd::mem_fun, butboost::bindwill allow you a bit more generality.In this case, you would define
FindNonUniqueValuesas something like:Here, I’m not really sure how your
FindNonUniqueValuesgets its list of objects (or exactly what it’s supposed to return – is anIEnumerablelike an iterator?), so you could fill that in.