Functors

R

raghu

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
 
R

Richard Bos

raghu said:
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
 
I

Ian Collins

raghu said:
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.
 
C

Chris Dollin

raghu said:
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.
 
R

Richard Bos

Ian Collins said:
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
 
I

Ian Collins

Ian said:
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;
};
 
R

Richard Bos

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

Richard, g,d,rlb
 
O

osmium

raghu said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Tasks in linux 7
Functors 2
Asp.net interactive map 0
How can I view / open / render / display a pdf file with c code? 0
OpenMP and functors 1
Solving nonlinear algebraic systems 1
functors 2
macros 2

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,836
Latest member
BuyBlissBitesCBD

Latest Threads

Top