Can I create a wrapper around std::find_if that passes in a comparator?

This topic is 1331 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I want to be able to search a private vector, without exposing the vector to the caller, using a custom comparator function that is passed in as a parameter. This seems like it should be possible, since I'm basically just creating a wrapper around a function that already exists, but I'm having trouble. Could this work or is my approach flawed?

What I'm trying to do is something like this:

class Container
{
public:
int findObject(Comparator compare);

private:
std::vector<Object*> objectList_;
};

int Container::findObject(Comparator compare)
{
std::vector<int>::iterator itr;
itr = std::find_if(objectList_.begin(); objectList_.end(); compare)
{
// ...
}
}

I think what I'm not getting is what the Comparator parameter type should be. If I type it as a class, then I don't think I can pass in any arbitrary comparitor I want. It would have to be the same one all the time right?

Edited by noodlyappendage

Share on other sites

As you noticed, you don't actually know what type to expect, because there could in fact be a multitude of types that satisfy what you need.  You could be specific and require a std::function<bool(Object*)>.  (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.)  But that comes with a runtime cost and a somewhat less flexible interface.

Assuming there isn't some other difficulty that would prevent this, the typical solution to this type of problem is to use templates.  You'll accept any type whatsoever, as long as it satisfies your needs.  In this case, you need what std::find_if needs:  it should be callable, taking an Object pointer as its only parameter, and returning a bool.  Try this out:

class Container
{
public:
template <typename TComparator>
Object* findObject(TComparator compare);

private:
std::vector<Object*> objectList_;
};

template <typename TComparator>
Object* Container::findObject(TComparator compare)
{
auto itr = std::find_if(objectList_.begin(), objectList_.end(), compare);
return (itr != objectList_.end()) ? *itr : nullptr;
}


Note that this would all need to be inside your header file, because templates cannot be compiled independently of the calling code, since the types aren't known until a piece of code makes the call.  Therefore the calling code actually needs full access to the source code in order to compile it.  However, once you get an iterator by calling std::find_if, if you still had a substantial amount of code to execute before returning a value, you could then call into another private member function that is defined in a .cpp file like normal, and thus keep the template bloat within your header file to a minimum.

(I used C++11 keywords auto and nullptr in the above code.  Depending on your compiler, you might need to switch back to fully specifying the type instead of using auto, and then returning NULL or 0 instead of nullptr.)

Edited by Andy Gainey

Share on other sites

You could be specific and require a std::function. (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.) But that comes with a runtime cost and a somewhat less flexible interface.

How so?  Do you have performance numbers? How is it less flexible to use a general-purpose standard way vs. a bespoke custom way?

Edited by Bregma

Share on other sites

You could be specific and require a std::function. (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.) But that comes with a runtime cost and a somewhat less flexible interface.

How so?  Do you have performance numbers? How is it less flexible to use a general-purpose standard way vs. a bespoke custom way?

Point taken on not having concrete performance information.  My recommendation is admittedly based pretty much just on theory.  Given that in this case the function object would only be called exactly once, the overhead of construction and destruction of that function object is just as significant as the call overhead.  And the construction overhead of function objects is very much dependent on library, compiler, and function to be wrapped.  In particular, memory might need to be allocated on the heap, and then promptly deallocated after only calling the function object once.

I believe that the template version fits this particular use case better, since it gives the caller the choice of how heavy or light the provided functor will be, and makes it a lot easier for the compiler to call the relevant code with the least amount of layers.  I suspect that different compilers and platforms would provide much more consistent behavior than using std::function for this particular kind of situation.

I also wouldn't consider the template solution to be a custom solution in contrast to a general purpose standard solution, as templates are one of C++'s two main ways of providing generic behavior, virtual functions and other function pointer-based solutions being the other way.  They each have their pros and cons, of course.  std::function might be favored if the function object was going to be stored somewhere and called later, possibly more than once, or if the findObject function were to live inside a dynamically linked library, but called from another library or executable.  But when the lifetime of the function object is incredibly short, it's called only once, and the code that uses the function object is relatively simple, the template solution feels far more natural to me as the default implementation choice, only to be switched to another style like std::function if in-practice evidence suggests it.  After all, what does the standard library itself do?  I'm not personally aware of a single function in the standard library that only accepts a std::function rather than being templated on the functor type.

Share on other sites

As you noticed, you don't actually know what type to expect, because there could in fact be a multitude of types that satisfy what you need.  You could be specific and require a std::function<bool(Object*)>.  (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.)  But that comes with a runtime cost and a somewhat less flexible interface.

Assuming there isn't some other difficulty that would prevent this, the typical solution to this type of problem is to use templates.  You'll accept any type whatsoever, as long as it satisfies your needs.  In this case, you need what std::find_if needs:  it should be callable, taking an Object pointer as its only parameter, and returning a bool.  Try this out:

class Container
{
public:
template <typename TComparator>
Object* findObject(TComparator compare);

private:
std::vector<Object*> objectList_;
};

template <typename TComparator>
Object* Container::findObject(TComparator compare)
{
auto itr = std::find_if(objectList_.begin(), objectList_.end(), compare);
return (itr != objectList_.end()) ? *itr : nullptr;
}


Note that this would all need to be inside your header file, because templates cannot be compiled independently of the calling code, since the types aren't known until a piece of code makes the call.  Therefore the calling code actually needs full access to the source code in order to compile it.  However, once you get an iterator by calling std::find_if, if you still had a substantial amount of code to execute before returning a value, you could then call into another private member function that is defined in a .cpp file like normal, and thus keep the template bloat within your header file to a minimum.

(I used C++11 keywords auto and nullptr in the above code.  Depending on your compiler, you might need to switch back to fully specifying the type instead of using auto, and then returning NULL or 0 instead of nullptr.)

Thank you! This works exactly how I need it to!

Share on other sites

You could be specific and require a std::function. (This method essentially performs type erasure, so that your findObject function doesn't need to care what the actual type is.) But that comes with a runtime cost and a somewhat less flexible interface.

How so?  Do you have performance numbers? How is it less flexible to use a general-purpose standard way vs. a bespoke custom way?

The type erasure requires wrapping the function behind some virtual function calls (or a function pointer at the least), which some common compilers today still will not optimize across even on maximum settings. Moving the body of your find function to the .cpp file will also require you to use LTO/WHOPR in order to inline the calls at all (but still requires devirtualization to run well). If your functor is large enough, it'll also fail to fit in a std::function's buffer and hence require allocation/deallocation to store. The flexibility claims are not necessarily valid; I can't think of anything you can give the template you can't give a std::function.

Overall, std::function is a fantastic tool to use when you need its features. The "bespoke custom way" is also quite usable and definitely not completely superseded by std::function.

• 10
• 11
• 9
• 16
• 19