Opened 10 years ago

Closed 10 years ago

#8024 closed Bugs (fixed)

Unable to create const_iterator for coroutine

Reported by: Nathan Ridge Owned by: olli
Milestone: To Be Determined Component: coroutine
Version: Boost 1.53.0 Severity: Problem
Keywords: Cc:

Description

It seems to be impossible to create a const_iterator for a coroutine. For example, the following fails to compile:

#include <boost/coroutine/coroutine.hpp>

typedef boost::coroutines::coroutine<int()> coroutine_type;

void f(coroutine_type::caller_type& c) {}

int main()
{
    coroutine_type c(f);
    coroutine_type::const_iterator it = boost::begin(c);
}

with the error:

test.cpp: In function 'int main()':
test.cpp:10:55: error: conversion from 'boost::range_iterator<boost::coroutines::coroutine<int()> >::type {aka boost::coroutines::detail::coroutine_op<int(), boost::coroutines::coroutine<int()>, int, 0>::iterator}' to non-scalar type 'boost::coroutines::detail::coroutine_op<int(), boost::coroutines::coroutine<int()>, int, 0>::const_iterator' requested

Change History (3)

comment:1 by olli, 10 years ago

Resolution: fixed
Status: newclosed

small fix in code but use 'boost::const_begin()' to create an const_iterator, please

comment:2 by Nathan Ridge, 10 years ago

Resolution: fixed
Status: closedreopened

What I'm really trying to get to work is code like the following:

#include <iostream>
#include <boost/coroutine/coroutine.hpp>

typedef boost::coroutines::coroutine<int()> coro_t;

void foo(coro_t::caller_type& caller)
{
    caller(0);
    caller(1);
    caller(2);
}

coro_t f()
{
    return coro_t(&foo);
}

template <typename Range>
void print_range(const Range& range)
{
    for (auto element : range)
        std::cout << element;
}

int main()
{
    print_range(f());
}

The C++11 range-based for loop requires either that the range type have a member function named 'begin', or that there exist a non-member function named 'begin' that is found by argument-dependent lookup (i.e. in the same namespace as the range type). In either case, the 'begin' function should have a const version that returns a const iterator.

What that translates to for coroutines is that either coroutine needs to have a member function with the signature "begin() const" or there needs to be a non-member function named "begin" in "boost::coroutines" that takes a "const coroutine&" parameter. (And of course the same for 'end').

comment:3 by olli, 10 years ago

Resolution: fixed
Status: reopenedclosed

fixed for C++11 too

Note: See TracTickets for help on using tickets.