Boost logo

Boost Users :

Subject: Re: [Boost-users] Using Coroutines in the Visitor Pattern (use-case: BGL event visitor)
From: alex (alexhighviz_at_[hidden])
Date: 2016-01-19 17:13:33


> -----Original Message-----
> From: Boost-users [mailto:boost-users-bounces_at_[hidden]] On Behalf
Of
> Nat Goodspeed
> Sent: 19 January 2016 21:25
> To: boost-users_at_[hidden]
> Subject: Re: [Boost-users] Using Coroutines in the Visitor Pattern
(use-case: BGL
> event visitor)
>
> On Tue, Jan 19, 2016 at 6:02 AM, alex <alexhighviz_at_[hidden]> wrote:
>
> >>> I can see in the documentation that the constructor enters the
> >>> coroutine-
> >>function, but it is not clear to me why. Would it not have been neater
> >>if this was avoided?
>
> >>With a pull_type coroutine, the expectation is that every time it
> >>suspends (until it exits), it has produced a value for its consumer.
> >>If the coroutine constructor didn't enter the coroutine-function, the
> >>first invocation would have to be a special case.
>
> > To me it seems that the first invocation may either exit or produce a
> > value, just as each subsequent invocation. I don't see the why the
> > first invocation is special.
>
> Okay, so let's consider this snippet:
>
> boost::coroutines::asymmetric_coroutine<int>::pull_type
source(somefunc);
> while (source)
> {
> std::cout << source.get() << std::endl;
> source();
> }
>
> Suppose the asymmetric_coroutine<int>::pull_type constructor did not enter
> somefunc(). How would the operator bool() call invoked by the while
statement
> know whether there's a value?
>

OK, if the constructor would not enter the function, I would use it like
this:

boost::coroutines::asymmetric_coroutine<int>::pull_type source(somefunc);
while (source())
{
  std::cout << source.get() << std::endl;
}

I expect the following in the evaluation of the while condition

1.The operator () will enter somefunc().
2. somefunc() will either complete or produce an int, the state of source
will reflect this
3. The operator() returns a reference to source.
4. If the function has completed the bool() function will cast it to false,
if the function has returned an int the bool() function will cast it to
true.

> >> Even if that were
> >>desirable, how could you distinguish the case in which the coroutine
> >>produces zero values?
>
> > That case would just never enter the while loop.
>
> But again: how do we know?

Operator() will cause somefunc() to complete and hence bool() will return
false: source.get() will never be called.
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users


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