c++templatestypesdefaultdefault-arguments

How to understand default template argument in C++11?


Here is the example I got from C++ Primer ver5 Chapter 16:

template <typename U, typename F=std::less<U>>
int compare(const U& v1, const U& v2, F f = F())
{
    if (f(v1, v2)) return -1;
    if (f(v2, v1)) return 1;
    return 0;
}

I am a little confused of the usage here:

typename F=std::less<U>

Based on my understanding, typename F refers to F is a kind of type here, how could we assign a function object std::less to it? If so, does it mean I can also do sth like auto F = std::less<U>; or typedef F std::less<U>?

Or maybe should I consider it as using F = std::less<U>?


Solution

  • how could we assign a function object std::less to it?

    std::less<U> is not a function object, but a class type that acts as a function object type. See e.g. the reference at cppreference.com.