1

I was reading something about default template argument on the book, and this piece of code made me really confused

template<typename T, typename F = less<T>>
bool compare(const T& v1, const T& v2, F f = F())
{
    return f(v1, v2);
}

The book says F represents the type of callable object and bind f to it. But how can F be that type?

I don't understand the meaning of F f=F(), and if I pass my own compare function to the template, it works, how can it deduce F from function?

JeJo
  • 30,635
  • 6
  • 49
  • 88
codesavesworld
  • 587
  • 3
  • 10

1 Answers1

1

I don't understand the meaning of F f=F() [...]

This is how you provide default argument to function parameter in C++. Just like we do, any normal functions; let's say

void func1(int i = 2)     {/*do something with i*/}
//         ^^^^^^^^^
void func2(int i = int()) {/*do something with i*/}
//         ^^^^^^^^^^^^^
void func3(int i = {})    {/*do something with i*/}
//         ^^^^^^^^^^

Which allows the above functions to be called, with the argument

func1(1); //---> i will be initialized to 1
func1(2); //---> i will be initialized to 2
func1(3); //---> i will be initialized to 3

or without the argument provided.

func1(); //---> i will be initialized to 2
func2(); //---> i will be initializedto 0
func3(); //---> i will be initialized to 0

Similar manner compare can be called without the third parameter like

compare(arg1, arg2) // ---> f will be `std::less<T>` here, which is the default argument

or with the third parameter

compare(arg1, arg2, [](const auto& lhs, const auto& rhs){ return /*comparison*/;});
//                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ some comparison function here
JeJo
  • 30,635
  • 6
  • 49
  • 88
  • thanks,but in typename F=less,isn't F a class type? if I pass a function or lambda to it ,why does it work? – codesavesworld Oct 18 '19 at 05:09
  • @zxy1122 [`std::less`](https://en.cppreference.com/w/cpp/utility/functional/less) is a struct defined with `operator()`, in other words a [functor](https://stackoverflow.com/questions/356950/what-are-c-functors-and-their-uses). It is a callable like functions or lambdas. And the only condition here is that the third parameter to the function should be callable, more precisely a binary predicate. The `std::less`, or passed functions or lambdas, if they meet the requirement, and hence your function works. – JeJo Oct 18 '19 at 05:13
  • I read the link you gave,so the typename of F is a functor and when I pass function or lambda to template,it wil convert it to a functor? do I understant the whole thing correctly? – codesavesworld Oct 18 '19 at 06:22
  • 1
    @zxy1122 First of all, `F` is a template argument, meaning it's instatiation depends on what type you want to instantiate with this. When no arguments will be passed, the type of `F` will be the type of the (default argument) **`std::less`**, which is a **functor**. When a **lambda** passed, the `F` of the type of **`decltype(lambda passed)`**. When a **function**, `F` get deduced to **function pointer(i.e. `bool(*)(type, type)`**. That means the type of `F` depends on what you providing to the `compare`, to instantiate. Hope that clear now! – JeJo Oct 18 '19 at 06:27