I would like to have a template function contains
which has two possible definitions, depending on the second type:
template <typename ElementType, typename CollectionType>
bool contains(const CollectionType & collection, ElementType element)
{
return collection.end() != std::find(collection.begin(), collection.end(), element);
}
and
template <typename CollectionType, typename PredicateType>
bool contains(const CollectionType & collection, PredicateType predicate)
{
return collection.end() != std::find_if(collection.begin(), collection.end(), predicate);
}
The standard library often uses _if to distinguish predicate from value versions of algorithms - such as find
and find_if
.
Ideally, I'd like the compiler to figure out which one to use. After all, the above two templates are intended for very different uses - the types of ElementType and PredicateType are pretty different domains.
Is there a good way to accomplish this via template shenanigans? Or am I stuck with finding two different names for the above contains functions?