This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: mutually-recursive types and an old puzzle
- From: Joe Buck <Joe dot Buck at synopsys dot COM>
- To: Gabriel Dos Reis <gdr at acm dot org>
- Cc: Chris Lattner <sabre at nondot dot org>, gcc at gcc dot gnu dot org
- Date: Wed, 30 Jun 2004 12:22:38 -0700
- Subject: Re: mutually-recursive types and an old puzzle
- References: <Pine.LNX.4.44.0406291930070.1537-100000@nondot.org> <32877.::ffff:128.194.146.36.1088556186.squirrel@webmail.nerim.net> <20040629175356.A11677@synopsys.com> <32929.::ffff:128.194.146.36.1088557164.squirrel@webmail.nerim.net>
On Wed, Jun 30, 2004 at 02:59:24AM +0200, Gabriel Dos Reis wrote:
> I just gave a hint to why the fixpoint operator of C++ may suffice
> to solve the puzzle -- it wasn't an final solution.
Here's one, if anyone's curious. The class has one member, which is
a function pointer; the operator() overload calls the function pointer.
---------------------------------------------------
#include <iostream>
class FsmState {
public:
typedef class FsmState (*funcpointer)(int);
FsmState(funcpointer pfunc) : m_func(pfunc) {}
FsmState operator()(int inp) const { return (*m_func)(inp);}
private:
funcpointer m_func;
};
FsmState state_1(int inp);
FsmState state_0(int inp) {
std::cout << "0\n";
return inp ? &state_1 : &state_0;
}
FsmState state_1(int inp) {
std::cout << "1\n";
return inp ? &state_0 : &state_1;
}
int main(int,char**) {
FsmState state(&state_0);
int inp;
for (unsigned i = 0; i < 10; i++) {
std::cout << "Next input: ";
std::cin >> inp;
state = state(inp);
}
}
---------------------------------------------------