I'm trying to understand the difference in the following two cases. In both, I introduce a comparator as a lambda function:
auto comp = [](int x, int y) { return x%2 < y%2; };
But then, if I want to use it with a sort, I can simply write
sort(vec.begin(), vec.end(), comp);
while for a class templated on a comparator, e.g. a priority_queue I need a wrapper:
priority_queue<int, decltype(comp)> my_queue;
to pass it as a template parameter.
The sort function itself is templated on a comparator class, yet it's somehow works without a decltype. Is there an implicit conversion that works for a sort, but doesn't work in the second case?
<>are types, and the things inside of()are instances of typessort(..., comp);takes an instance of a comparator as an input parameter.compis an object instance, whose type is defined by the compiler. Whereaspriority_queue<..., decltype(comp)>takes a type of a comparator as a template argument. You can't pass an object instance in a template, but you can pass its type.