Jack  Chin
Jack Chin

Reputation: 525

why std::less<int>() is a function object

why is std::less<int>() a function object as used in

std::sort(vec.begin(),vec.end(),std::less<int>());

but std::less<int> is a type and operator is function call, there is no object been created, or memory address we can reference

Upvotes: 3

Views: 2276

Answers (2)

Joker_vD
Joker_vD

Reputation: 3775

std::less<int>() is a constructor call. It creates a new std::less<int> object which, yes, has overloaded operator().

Upvotes: 8

Alexander Gessler
Alexander Gessler

Reputation: 46647

std::less<int>() does actually create a temporary instance of std::less that has a memory address (even though it is empty and therefore won't occupy any real memory with any sane compiler). sort keeps this instance around and uses its overloaded operator () to perform comparisons.

std::less<int>()(a, b) would directly perform a comparison between two integers in case the use of both object creation syntax and operator() calls is what confused you.

Upvotes: 3

Related Questions