Functors

Discussion in 'C Programming' started by raghu, Jul 20, 2007.

  1. raghu

    raghu Guest

    Hello Everyone,

    I got a document which says functors is a function with a
    state and it can be implemented in C++. Can't we implement functors
    in C language.

    Awaiting for your Reply

    Thanking you

    -Raghu
    raghu, Jul 20, 2007
    #1
    1. Advertising

  2. raghu

    Richard Bos Guest

    raghu <> wrote:

    > I got a document which says functors is a function with a
    > state and it can be implemented in C++. Can't we implement functors
    > in C language.


    Of course it can, one way or another. The question is, what kind of
    behaviour precisely do you want from this "functor"? Once you know that,
    you can start implementing.

    Richard
    Richard Bos, Jul 20, 2007
    #2
    1. Advertising

  3. raghu

    Ian Collins Guest

    raghu wrote:
    > Hello Everyone,
    >
    > I got a document which says functors is a function with a
    > state and it can be implemented in C++. Can't we implement functors
    > in C language.
    >

    Not in the same way as they are implemented in C++. A functor is an
    object, a function even though it has an address is not, at least not in C.

    A C function can have state in the form of static variables, but you can
    not have more than one instance of a function.

    --
    Ian Collins.
    Ian Collins, Jul 20, 2007
    #3
  4. raghu

    Chris Dollin Guest

    raghu wrote:

    > I got a document which says functors is a function with a
    > state and it can be implemented in C++. Can't we implement functors
    > in C language.


    Not if you want to use function-call syntax for them. That is, if F is
    a functor object (in C, that would be a struct containing a function
    pointer and some state), then you'd like to be able to write

    F( someArgs )

    but you can't [1]: you'll have to write something more like:

    call( F, someArgs )

    which when you have nested functor calls becomes eg

    call( F, call(G, someArgs) )

    This is not mentioning that to make this work well you want something
    like polymorphic types, which C++ sort-of has but C definitely-hasn't.

    [1] In C. In C++ you can overload the () operator. As usual, Opinion
    Is Divided on whether this is a Good Thing or a Bad Thing or a
    Call Of Cthulhu complete with tentacles and loss of SAN.

    --
    Chris "/H. P./ Lovecraft ..." Dollin

    Hewlett-Packard Limited Cain Road, Bracknell, registered no:
    registered office: Berks RG12 1HN 690597 England
    Chris Dollin, Jul 20, 2007
    #4
  5. raghu

    Richard Bos Guest

    Ian Collins <> wrote:

    > raghu wrote:
    > > I got a document which says functors is a function with a
    > > state and it can be implemented in C++. Can't we implement functors
    > > in C language.
    > >

    > Not in the same way as they are implemented in C++. A functor is an
    > object, a function even though it has an address is not, at least not in C.
    >
    > A C function can have state in the form of static variables, but you can
    > not have more than one instance of a function.


    There are ways around this, but calling the "functor" becomes slightly
    more complicated.

    Richard
    Richard Bos, Jul 20, 2007
    #5
  6. raghu

    Ian Collins Guest

    Ian Collins wrote:
    > raghu wrote:
    >> Hello Everyone,
    >>
    >> I got a document which says functors is a function with a
    >> state and it can be implemented in C++. Can't we implement functors
    >> in C language.
    >>

    > Not in the same way as they are implemented in C++. A functor is an
    > object, a function even though it has an address is not, at least not in C.
    >
    > A C function can have state in the form of static variables, but you can
    > not have more than one instance of a function.
    >

    On reflection, you could mimic a C++ functor in the same way as any
    other struct with a member function by using a struct with a function
    pointer as a member.

    typedef struct functor Functor;
    typedef void (Fn)( Functor* );

    struct functor
    {
    int state;
    Fn* function;
    };

    --
    Ian Collins.
    Ian Collins, Jul 20, 2007
    #6
  7. raghu

    Richard Bos Guest

    Chris Dollin <> wrote:

    > --
    > Chris "/H. P./ Lovecraft ..." Dollin
    >
    > Hewlett-Packard Limited


    Yes... we know. Anyone of us who's installed a Windows HP driver,
    anyway. Score -5 SAN.

    Richard, g,d,rlb
    Richard Bos, Jul 20, 2007
    #7
  8. raghu

    osmium Guest

    "raghu" writes:

    > I got a document which says functors is a function with a
    > state and it can be implemented in C++. Can't we implement functors
    > in C language.


    You can provide state in a C program by the use of static variables.
    Whether this is a functor or not is a separate issue. What does the document
    at issue define as a functor?

    I think the number of static variables, that is the "complexity" of state,
    is a good indicator of when a C++ class should be used instead of C coding
    techniques. I doubt I would ever write a C function that had more than one
    static variable; I would write a C++ class.

    With my background I think of a functor as being basically a C++ member
    function with the signature foo( <argument-list>).
    I think the question posed leads you into the pointless and familiar
    labyrinth of "can we write object oriented programs in C?", A place where I
    think a lot of time has been wasted; you will always come back to
    *definitions*. What in hell are the definitions of the terms being used in
    the discussion? Answer: they are vague and/or different for the different
    people involved in the discussion. IOW, all semantics and very little
    useful substance.
    osmium, Jul 20, 2007
    #8
    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:
    383
    Dhruv
    Jul 3, 2003
  2. Rob Williscroft

    Re: Need help with generic functors....

    Rob Williscroft, Aug 17, 2003, in forum: C++
    Replies:
    3
    Views:
    341
    Gordon Scott
    Aug 18, 2003
  3. red floyd
    Replies:
    0
    Views:
    313
    red floyd
    Nov 13, 2003
  4. nsgi_2004

    functors

    nsgi_2004, Aug 12, 2004, in forum: C++
    Replies:
    2
    Views:
    457
    Karl Heinz Buchegger
    Aug 12, 2004
  5. Satish
    Replies:
    1
    Views:
    773
    David Hilsee
    Sep 10, 2004
Loading...

Share This Page