Boost logo

Boost Users :

Subject: Re: [Boost-users] self-referential boost functions
From: Jeremiah Willcock (jewillco_at_[hidden])
Date: 2012-06-28 13:47:33


On Thu, 28 Jun 2012, Philipp Moeller wrote:

> Adam Crain <jadamcrain_at_[hidden]> writes:
>
>> Inquiring as to whether this is possible or if there is a better pattern
>> for simulating this behavior...
>>
>> Is it possible for boost::function to be self referential? I.e. how do I
>> simulate behavior like the following:
>>
>> typedef boost::function<Func (int)> Func;
>>
>> A function that does something mutable and then returns the next function
>> to be called or an empty function.
>
> Boost.Function still requires static typing on the return type and that
> type depends on some runtime variable, so you cannot statically type it.

What about something like:

struct t: boost::function<t()> {
   explicit t(boost::function<t()> f): boost::function<t()>(f) {}
};

t my_f() {return t(my_f);}

int main(int, char**) {
   t x(my_f);
   return 0;
}

You can also try removing the "explicit" from the constructor or add
variadic constructor templates to make the syntax act more like
boost::function<t()>.

-- Jeremiah Willcock


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net