473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Function that returns a pointer to a function of the same signature?

Hiya. I've just been implementing a finite state machine a couple of
times to compare the performance of different approaches. One of my
attempts was to have a function for each state, which takes a single
character of input and returns a function pointer to the next state.
This presented an interesting challenge when I tried to code it. ;)
"typedef statefn* (statefn)(int);" is the closest I've got, but of
course you can't refer to the new type within the typedef itself.
I ended up just returning void* and noted this implementation was
terrible[1] compared to the switch and goto solutions, but it's nagging
me that I didn't work out how to define the function properly.
So... is it possible? :)
(I found a C++ solution at http://www.gotw.ca/gotw/057.htm but nothing
in straight C)

[1] Mind you, my state machine only had 3 states, so the advantage of
the function method has of jumping straight to the state code instead
of having to iterate over states to work out which one we are in (as in
the switch method) wouldn't have had a chance to shine. Also I expect
much better performance could be obtained by passing the input stream
to the function so it only has to return when it needs to change state.

Jan 11 '06 #1
2 4249
Ico
sqweek <sq****@gmail.com> wrote:
Hiya. I've just been implementing a finite state machine a couple of
times to compare the performance of different approaches. One of my
attempts was to have a function for each state, which takes a single
character of input and returns a function pointer to the next state.
This presented an interesting challenge when I tried to code it. ;)
"typedef statefn* (statefn)(int);" is the closest I've got, but of
course you can't refer to the new type within the typedef itself.
I ended up just returning void* and noted this implementation was
terrible[1] compared to the switch and goto solutions, but it's nagging
me that I didn't work out how to define the function properly.
So... is it possible? :)


Not in a simple way. This is a frequently asked question, you can find the
FAQ answer at http://c-faq.com/decl/recurfuncp.html

--
:wq
^X^Cy^K^X^C^C^C^C
Jan 11 '06 #2
sqweek wrote:
Hiya. I've just been implementing a finite state machine a couple of
times to compare the performance of different approaches. One of my
attempts was to have a function for each state, which takes a single
character of input and returns a function pointer to the next state.
This presented an interesting challenge when I tried to code it. ;)
"typedef statefn* (statefn)(int);" is the closest I've got, but of
course you can't refer to the new type within the typedef itself.
I ended up just returning void* and noted this implementation was
terrible[1] compared to the switch and goto solutions, but it's nagging
me that I didn't work out how to define the function properly.
So... is it possible? :)
(I found a C++ solution at http://www.gotw.ca/gotw/057.htm but nothing
in straight C)

[1] Mind you, my state machine only had 3 states, so the advantage of
the function method has of jumping straight to the state code instead
of having to iterate over states to work out which one we are in (as in
the switch method) wouldn't have had a chance to shine. Also I expect
much better performance could be obtained by passing the input stream
to the function so it only has to return when it needs to change state.


See http://smc.sourceforge.net which generates state machine code for a
number of languages including C. It uses a simple language to specify
the state machine - but this simple language supports a number of UML
state design ideas.

Charles Rapp

Jan 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Suzanne Vogel | last post by:
Is it possible to store a pointer to a template function whose template type is not known until runtime? eg, Can I store a pointer to this? template<class T> f() {...} I know how to store...
6
by: Brian Ross | last post by:
Hi, I am trying to do something similar to the following: int Func1(int x, int y); double Func2(double x, double y); template <typename FuncT> // <- What would go here class CObjectT {
10
by: Martin Vorbrodt | last post by:
Example code in one of my books intrigues me: class B { public: B* Clone() const { B* p = DoClone(); assert(typeid(*p) == typeid(*this)); return p; }
5
by: Alfonso Morra | last post by:
Hi all, I came accross this FP signature and it has me baffled. Can anyone explain this signature to me - i.e what are the input and output types ? void(*(*foo)(int))(int,void(*)(int)) ...
2
by: Kurt Ng | last post by:
Hi, y'all. Can anyone help me on this problem? I'm working with a third-party C dll, and I'm having trouble importing into C# the dll's methods that return one of the dll's defined types,...
8
by: Johann Blake | last post by:
I need to call a C DLL function. The first parameter expects a pointer to a long. It returns a value at the address of the pointer. The second parameter expects a pointer to a pointer. It creates...
5
by: shaun roe | last post by:
I am about to replace a plain function which returns a string with an object; I am trying to preserve the interface to clients of the function, while imbuing the function with some internal state....
5
by: Squeamizh | last post by:
The subject line probably isn't the best, but I don't know how else to describe my question. When I try to compile the code below, my compiler reports an error (error text is below code sample)....
9
by: Christian Christmann | last post by:
Hi, I was just going through this exercise http://www.cas.mcmaster.ca/~franek/books/membook-answers/ch4/answers-ch4-3.html and I'am confused about the answer. It says: "... the compiler...
45
by: noridotjabi | last post by:
What is the purpose of the function pointer? Why do you need a pointer to a function. I cannot really think of any application where this is the only or even easiest solution to a problem. I'm...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.