functors

Discussion in 'C++' started by nsgi_2004, Aug 12, 2004.

  1. nsgi_2004

    nsgi_2004 Guest

    Hi,

    I have been learning about functors and at first everything was clear. Make
    a class and overload operator () so that an object of the functor can be
    thought of as a function.

    However, I have seen in Effective STL, Scott passes normal functions into
    places that expect functors. I'm not sure how this works, as I thought
    functors had to be classes.

    The only thing I can think of is that, because the STL algorithms are
    templated, the template algorithm function will create a function with a
    parameter that takes a pointer to the function being passed in as a functor.
    I suppose now that I think about it, the compiler would know the function
    type and could do this. If so this is very cool, because for a long time
    I've needed this sort of functionality and had to resort to weird hacks.

    Thanks in advance.
     
    nsgi_2004, Aug 12, 2004
    #1
    1. Advertising

  2. nsgi_2004

    David Hilsee Guest

    "nsgi_2004" <> wrote in message
    news:RMASc.27737$ih.15231@fed1read07...
    > Hi,
    >
    > I have been learning about functors and at first everything was clear.

    Make
    > a class and overload operator () so that an object of the functor can be
    > thought of as a function.
    >
    > However, I have seen in Effective STL, Scott passes normal functions into
    > places that expect functors. I'm not sure how this works, as I thought
    > functors had to be classes.
    >
    > The only thing I can think of is that, because the STL algorithms are
    > templated, the template algorithm function will create a function with a
    > parameter that takes a pointer to the function being passed in as a

    functor.
    > I suppose now that I think about it, the compiler would know the function
    > type and could do this. If so this is very cool, because for a long time
    > I've needed this sort of functionality and had to resort to weird hacks.
    >
    > Thanks in advance.


    Thanks for what? It sounds like you've got a solid grip on how one can
    either pass an instance of a class or a pointer to a function to a template
    function.

    void function();

    struct Functor {
    void operator()();
    };

    template <class Func>
    void foo( Func f ) {
    f();
    }

    int main() {
    // instantiates the template with an argument of void (*)()
    // (a pointer to a function)
    foo( &function );

    // instantiates the template with an argument of Functor
    foo( Functor() );
    }

    The latter can result in more efficient code because the compiler knows
    exactly what function will be called for that instantiation and can
    therefore inline it or otherwise optimize for it in the generated code.

    Do you have any questions about this?

    --
    David Hilsee
     
    David Hilsee, Aug 12, 2004
    #2
    1. Advertising

  3. nsgi_2004 wrote:
    >
    > Hi,
    >
    > I have been learning about functors and at first everything was clear. Make
    > a class and overload operator () so that an object of the functor can be
    > thought of as a function.
    >
    > However, I have seen in Effective STL, Scott passes normal functions into
    > places that expect functors. I'm not sure how this works, as I thought
    > functors had to be classes.


    Yep.

    The thing is:

    When the compiler tries to compile something like this:

    foo();

    Then there are 2 ways it can be done.
    Either foo is a function,

    void foo()
    {
    }

    then the above compiles as ordinary function call.
    Or foo is an object with an operator(). Then the above
    compiles as calling the member operator() on object foo.

    The piece that you see to be missing is that what you have
    seen works with templates only. And the important part is,
    that a template in itself is just a sort of blueprint. Only
    after you specify the template parameters, the compiler takes
    the template code, substitutes the template parameters everywhere
    and compiles it. So when it comes to compilation, the compiler
    already knows which one of the 2 versions from above applies.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Aug 12, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul MG
    Replies:
    2
    Views:
    405
    Dhruv
    Jul 3, 2003
  2. Rob Williscroft

    Re: Need help with generic functors....

    Rob Williscroft, Aug 17, 2003, in forum: C++
    Replies:
    3
    Views:
    359
    Gordon Scott
    Aug 18, 2003
  3. red floyd
    Replies:
    0
    Views:
    341
    red floyd
    Nov 13, 2003
  4. Satish
    Replies:
    1
    Views:
    808
    David Hilsee
    Sep 10, 2004
  5. Michael

    functors and stl find_if

    Michael, Feb 19, 2005, in forum: C++
    Replies:
    4
    Views:
    6,310
    SnaiL
    Feb 19, 2005
Loading...

Share This Page