Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#7988 closed Bugs (fixed)

[Coroutine] Support for C++11 range-for is missing

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

Description

Using a snapshot of the upcoming GCC 4.8 release and Boost 1.53, the following does not compile:

#include <boost/coroutine/all.hpp>

int main()
{
    using coro = boost::coroutines::coroutine<int()>;
    for(int i: coro { [](coro::caller_type& yield) { yield(42); } }) {
        // unused
        static_cast<void>(i);
    }
}

As far as I can tell, this is because while Boost.Coroutine does provide functionality for e.g. boost::begin(c) and boost::end(c), it does not provide for the similar, ADL-enabled uses like begin(c) and end(c), where begin and end are in scope.

As it so happens, boost::begin and boost::end do ADL on behalf of the users -- their Standard counterparts do not, and a range-for statement is specified to make use of unqualified calls to begin and end.

To substantiate my hypothesis, adding begin and end function templates in e.g. namespace boost::coroutines does appear to make the previous snippet compile, and more involved examples behave as expected.

// Try adding this:
namespace boost { namespace coroutines {

template<typename Sig> auto begin(coroutine<Sig>& c)
-> decltype(boost::begin(c))
{ return boost::begin(c); }

template<typename Sig> auto end(coroutine<Sig>& c)
-> decltype(boost::end(c))
{ return boost::end(c); }

} }

Change History (6)

comment:1 by Michel Morin, 10 years ago

Component: Nonecoroutine
Owner: set to olli

comment:2 by olli, 10 years ago

Resolution: fixed
Status: newclosed

comment:3 by anonymous, 9 years ago

The range-for does not work with Clang 3.4, boost 1.55.0. The unidirectional coroutine example fails to compile, with the following message.

error: invalid range expression of type 'boost::coroutines::pull_coroutine<int>'; no viable 'begin' function available

For now wrapping with boost::make_iterator_range(my_coro) will solve the problem, but a cleaner solution would be well appreciated.

comment:4 by olli, 9 years ago

fix for clang-c++11 added

comment:5 by anonymous, 8 years ago

The problem is in fact *not* fixed, see http://lists.boost.org/boost-users/2014/08/82815.php

in reply to:  5 comment:6 by olli, 8 years ago

Replying to anonymous:

The problem is in fact *not* fixed, see http://lists.boost.org/boost-users/2014/08/82815.php

example/cpp11/asymmetric/fibonacci compiles with gcc-4.8.1, clang-3.4.1, msvc-11 without problems

b2 toolset=gcc cxxflags="-std=c++11"
b2 toolset=clang cxxflags="-std=c++11"
b2 toolset=msvc-11
Last edited 8 years ago by olli (previous) (diff)
Note: See TracTickets for help on using tickets.