This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: mutually-recursive types and an old puzzle


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);
    }
}
---------------------------------------------------


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]