Boost logo

Boost :

Subject: [boost] [coroutine] interface suggestion
From: Oliver Kowalke (oliver.kowalke_at_[hidden])
Date: 2012-09-19 03:30:32

my suggestion:

1. the lib provides both coroutine<> and generator<>

2. coroutine<> is special routine allowing to enter and leave a function
    multiple times

3. coroutine<> first template arg defines the signature of the function
   accepted by coroutine<>

4. coroutine<>::operator() return type is equal to the return type of signature (template arg)

5. function accepted by coroutine<> first arg allows to suspend coroutine<>
   == jump back to caller of coroutine<>::operator()
   -> it has to be discussed if this type is of coroutine<> with an
      'inverted' signature (as proposed by Giovanni) or not

6. generator<> returns a set of values in a sequence

7. generator<> has only one template arg defining the return type

8. function accepted by generator<> is required to return void

9. generator<>::operator() returns optional< return_type >

10. generator<> is not a range because
    - 'A Range provides iterators for accessing a half-open range
      [first,one_past_last) of elements and provides information
      about the number of elements in the Range.'
      -> a generator can not determine how many elements it will return
    - generator<> can only provide a single pass:
      'For any Range a, [boost::begin(a),boost::end(a)) is a valid range,
       that is, boost::end(a) is reachable from boost::begin(a) in a
       finite number of increments.'
      -> if the function accepted by generator<> loops for ever it
         violates the range invariant

        typedef generator< int > gen_t;
        void f( gen_t::caller_t & caller) {
           while ( true) {
               caller( 7);

11. on top of generator<> something like an iterator could be provided:
    typedef generator< int > gen_t;
    gen_t gen( f);
    enumerator< gen_t > i( gen):
    enumerator< gen_t > e;
    while ( i != e) {
      cout << * i++ << "\n";


Boost list run by bdawes at, gregod at, cpdaniel at, john at