In the first case, you can create
template<class T> struct CompareByIntProperties { CompareByIntProperties(vector<T::*int> props) : props_(props) {} bool less_than(const T& a, const T& b) const { for (vector<T::*int>::const_iterator it = props_.begin(); it != props_.end(); ++it) { if (a.(**it) < b.(**it)) return true; if (a.(**it) > b.(**it)) return false; } return false; } vector<T::*int> props_; };
which will allow you
vector<Foo::*int> properties; if (compare_foo) properties.push_back(&Foo::foo); if (compare_bar) properties.push_back(&Foo::bar); if (compare_qux) properties.push_back(&Foo::qux); sort(container.begin(), container.end(), CompareByIntProperties<Foo>(properties));
Please forgive any syntax errors, none of them have been verified by compilation. But you have an idea.
In the second case, since you are invoking a static method, you do not have free ownership to configure the comparator in this way.
I would not worry about efficiency. If you do not get access to anything non-static, good C ++ compiler can speed up the creation / destruction of additional objects and, possibly, even embed a comparator.
source share