Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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>?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading