Suppose I define the following function template that removes an item from the given set if the specified function returns true for that item.
template<typename TYPE>
int removeItems(
QSet<TYPE>& set,
const std::function<bool (const TYPE&)>& shouldRemove)
{
int numRemoved = 0;
auto iter = set.begin();
while (iter != set.end())
{
if (shouldRemove(*iter))
{
iter = set.erase(iter);
++numRemoved;
}
else
{
++iter;
}
}
return numRemoved;
}
I can use this function template like this:
QSet<QString> stuff = {"A", "a", "B", "b", "C", "c"};
removeItems<QString>(stuff, [](const QString& thing) {
return thing.toLower() == "a";
});
qDebug() << stuff; // QSet("B", "b", "C", "c")
But can the function template be defined in such a way that the template argument can be omitted from the function call?
QSet<QString> stuff = {"A", "a", "B", "b", "C", "c"};
removeItems(stuff, [](const QString& thing) {
return thing.toLower() == "a";
});
qDebug() << stuff; // QSet("B", "b", "C", "c")