I try to understand the docs for std::less
, where the following example is given for the usage with a template.
#include <functional>
#include <iostream>
template <typename A, typename B, typename C = std::less<>>
bool fun(A a, B b, C cmp = C{})
{
return cmp(a, b);
}
int main()
{
std::cout
<< std::boolalpha
<< fun(1, 2) << ' ' // true
<< fun(1.0, 1) << ' ' // false
<< fun(1, 2.0) << ' ' // true
<< std::less<int>{}(5, 5.6) << ' ' // false: 5 < 5 (warn: implicit conversion)
<< std::less<double>{}(5, 5.6) << ' ' // true: 5.0 < 5.6
<< std::less<int>{}(5.6, 5.7) << ' ' // false: 5 < 5 (warn: implicit conversion)
<< std::less{}(5, 5.6) << ' ' // true: less<void>: 5.0 < 5.6
<< '\n';
}
Output:
true false true false true false true
My question is:
How is a std::
function used as a template argument?
And in this concrete case, why are there curly brackets {}
behind the C cmp = C
or in the call std::less<double>{}(5, 5.6)
?