Followers 0

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

## 5 posts in this topic

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
0

##### 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
2

##### 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
0

##### 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!

0

##### 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.
0

## Create an account

Register a new account