CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18

Thread: Functor

  1. #1
    Join Date
    May 2018
    Posts
    158

    Functor

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

    Code:
    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;
    }
    Code:
    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;
    }
    Here, I can't understand functor inside this function std::max(a, b, ComplexComparer());

    I understand ComplexComparer() is passed as parameter to function Max, its result will be Boolean: true or false.
    But ComplexComparer() has no parameters inside[B] ()[/B but

    bool operator()(const Complex& a, const Complex& b) has parameters!

    What parameter is passed as both 'a' and 'b' to ComplexComparer if inside parenthesis there is nothing ?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Functor

    std::max calls operator() [the same as other functions that call functors]. The passed args to operator() are a and b from std::max()
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2018
    Posts
    158

    Re: Functor

    Quote Originally Posted by 2kaud View Post
    std::max calls operator() [the same as other functions that call functors]. The passed args to operator() are a and b from std::max()
    Now I understand so

    std::max(a, b, ComplexComparer());

    these bold parameters are passed to ComplexComparer implicitly? How it's possible ?

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Functor

    Through the operator() which takes 2 args.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    May 2018
    Posts
    158

    Re: Functor

    Quote Originally Posted by 2kaud View Post
    Through the operator() which takes 2 args.
    OK, but I was expecting to something like this:

    see std::max(a, b, ComplexComparer(a,b));

    Where I can see that the "calling function" passes two parameters that is "a,b" ?
    Last edited by zio_mangrovia; June 1st, 2022 at 03:31 AM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Functor

    That is a ComplexComparer constructor that takes 2 args. If ComplexComparer had such a constructor it would be called first with the given constructor args, Then the operator() would be called with the required args.

    A functor automatically calls operator() with the required args. Note that since C++11 a lambda expression could be used in place of the class name.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    May 2018
    Posts
    158

    Re: Functor

    Quote Originally Posted by 2kaud View Post
    That is a ComplexComparer constructor that takes 2 args. If ComplexComparer had such a constructor it would be called first with the given constructor args, Then the operator() would be called with the required args.
    Excuse me ! I didn't see it. It'll be used the default constructor because it's not defined any customized constructor. Right?
    But the constructor creates ghost class ? There is no member but only the operator()

    A functor automatically calls operator() with the required args.
    Excuse me but in this case where I can see passed args to operator() in the "calling function" ?


    Note that since C++11 a lambda expression could be used in place of the class name.
    I know but I need to understand functor

  8. #8
    Join Date
    May 2018
    Posts
    158

    Re: Functor

    Quote Originally Posted by 2kaud View Post
    std::max calls operator() [the same as other functions that call functors]. The passed args to operator() are a and b from std::max()
    I was reading again, std::max calls operator() , which is not operator defined inside ComplexComparer class, ok?

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Functor

    which is not operator
    which is operator
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    May 2018
    Posts
    158

    Re: Functor

    Quote Originally Posted by 2kaud View Post
    std::max calls operator() [the same as other functions that call functors]. The passed args to operator() are a and b from std::max()
    I cannot understand this syntax because 3rd parameter of max function is the name of Class, It's not a Class instance.

  11. #11
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Functor

    Yes. It's a class name. What happens 'under the hood' is that the supplied class is instantiated using the default constructor and then from that object the operator() function is called with the supplied params.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  12. #12
    Join Date
    May 2018
    Posts
    158

    Re: Functor

    Quote Originally Posted by 2kaud View Post
    Yes. It's a class name. What happens 'under the hood' is that the supplied class is instantiated using the default constructor and then from that object the operator() function is called with the supplied params.
    I never saw iT this syntax that it's called the class name into function, where can I find doc about it ?
    If ComplexComparer has no members, what's instantiated about class?
    So when ComplexComparer() is called, this cause 2 called: default constructor + operator().
    My doubt is operator(), because inside parenthesis these is no given parameters.

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Functor

    If ComplexComparer has no members, what's instantiated about class?
    Whatever the default constructor does - if the class has no member variables then nothing.

    My doubt is operator(), because inside parenthesis these is no given parameters.
    std::max (along with most other algorithms) is template based. See https://en.cppreference.com/w/cpp/algorithm/max for examples of possible std::max implementations. In particular second version which takes a class as the third param.

    Code:
    template<class T, class Compare> 
    const T& max(const T& a, const T& b, Compare comp)
    {
        return (comp(a, b)) ? b : a;
    }
    In your example case, T is of type Complex and Compare is of type ComplexComparer.
    Last edited by 2kaud; June 3rd, 2022 at 07:50 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    May 2018
    Posts
    158

    Re: Functor

    Quote Originally Posted by 2kaud View Post
    Whatever the default constructor does - if the class has no member variables then nothing.



    std::max (along with most other algorithms) is template based. See https://en.cppreference.com/w/cpp/algorithm/max for examples of possible std::max implementations. In particular second version which takes a class as the third param.

    Code:
    template<class T, class Compare> 
    const T& max(const T& a, const T& b, Compare comp)
    {
        return (comp(a, b)) ? b : a;
    }
    In your example case, T is of type Complex and Compare is of type ComplexComparer.
    ok, I understand what you are saying but

    Code:
    const T& max(const T& a, const T& b, Compare comp)
    3rd paramater was expecting class instance ? Because I see 3d parameter ComplexComparer(), It's simply class name with (). Why ? I can understand for example:


    Code:
    ComplexComparer myclass;
    Complex m = std::max(a, b, myclass());

    even if I thought this solution because operator() overload says to having 2 arguments !
    Code:
    Complex m = std::max(a, b, ComplexComparer(a,b));

  15. #15
    Join Date
    May 2018
    Posts
    158

    Re: Functor

    Quote Originally Posted by 2kaud View Post
    std::max calls operator() [the same as other functions that call functors]. The passed args to operator() are a and b from std::max()
    I thank you very much. Can you give doc or link where I can find a similar case please ?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured