Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c++ Subject: Re: Tricky! Date: Tue, 30 Jul 2019 08:03:24 -0700 Organization: A noiseless patient Spider Lines: 69 Message-ID: <86imrj8szn.fsf@mailhub.linuxsc.com> References: <7995a414-14ec-47ae-8afc-9ebcc520e30d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="de0953fb1719bfdb1d66698494fc456b"; logging-data="4402"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+qKXmyMxyGsONh853Gksr/QPxAeL+szvU=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:6AHRMoMrI0uQkFi+6ClHC7Q2boE= sha1:5FFrRYHmXO9FNq4BAiKlHq7NvZg= Xref: csiph.com comp.lang.c++:67205 "Alf P. Steinbach" writes: > On 29.07.2019 15:30, <> Tiib wrote: > >> On Monday, 29 July 2019 14:49:58 UTC+3, Bonita Montero wrote: >> >>> I need a class-function / method that returns a function-pointer >>> to the same type of class-function. How do I implement this? I >>> need this to have a kind of state-machine where each state-func- >>> tion returns a pointer to the next state function. I.e I would >>> write: "while( (fn = state->*fn()) != nullptr );". I see no way >>> to define the type of fn since it is recursive. >> >> Perhaps there are no way to make a function that return function >> pointer of its own type. However I do not understand the need >> of it. > > I think she is inspired by (possibly someone else who is inspired > by) an article by the Go language creator. > > As I recall his examples work nicely in Go, or assembly language, or > a more dynamic language like Python, but have to be rewritten with > some indirection for a language with strong static type checking > like C++. The problem here is not strong static type checking but the lack of recursive types in C++. A language can have recursive types and also have strong static type checking. > The reason for doing it that way, in languages where it's simple, is > the textual simplicity of the source code. Since it doesn't give > that textual simplicity in C++, it's not a good way in C++. It's > doable, just not in any practical way. That depends on what is meant by "practical". It can be done like this: #include class State { using Nexter = ...; public: State(){} Nexter r0(){ return &State::r1; } Nexter r1(){ return &State::r2; } Nexter r2(){ return &State::rn; } Nexter rn(){ return 0; } }; void run_state_machine(){ State *s = new State; for( auto f = &State::r0; f; f = (s->*f)() ){ ::std::cout << "Got one\n"; } } with a fairly short (five-ish lines) elaboration for the "...". Personally I think this approach offers a fairly nice textual simplicity, and is eminently practical. If someone thinks five lines of overhead is too much, the "..." can be made into a template, giving using Nexter = WrapMemberFunctionPointer< State >; which provides an even nicer source text.