3rd parameter of function, as functor

about this code:

class ComplexComparer
{
public:
bool operator()(const Complex& a, const Complex& b);
};

bool ComplexComparer::operator()(const Complex& a, const Complex& b)
{
float magA = std::sqrt(a.getReal() * a.getReal() + a.getIm() * a.getIm());
float magB = std::sqrt(b.getReal() * b.getReal() + b.getIm() * b.getIm());
return magA < magB;
}

#include <iostream>
int main()
{
Complex a(1, 1.5);
Complex b(0.5f, 4);
Complex m = std::max(a, b, ComplexComparer());
std::cout << "max = " << m << std::endl;
return 0;
}

I cannot understand this syntax about functor, because 3rd parameter of std:max is ComplexComparer(), that is class name with parenthesis. Why ?
I'm expecting instance of ComplexComparer class, e.g. ComplexComparer test and test() as 3rd parameter;
Last edited on
> I'm expecting instance of ComplexComparer class

ComplexComparer() creates an anonymous temporary object of type ComplexComparer.

It is value initialised https://en.cppreference.com/w/cpp/language/value_initialization
Topic archived. No new replies allowed.