Я пробовал использование следующего исходного кода, но это не совместно. PrettyPrint-Override ">
Код: Выделить всё
#include
#include
#include
#include
namespace asio = boost::asio;
template
asio::experimental::coro count_chars_coro(Executor) {
size_t n = 0;
while(true) {
stype const next = co_yield n;
n += (next.size() + 1);
}
co_return n;
};
template
asio::awaitable count_chars_test() {
size_t n = 0;
auto gen = count_chars_coro(co_await asio::this_coro::executor);
n+= co_await gen.async_resume("abc", asio::use_awaitable);
n+= co_await gen.async_resume("def", asio::use_awaitable);
REQUIRE(n == 8);
co_return n+1;
};
TEST_CASE("asio coro generator") {
asio::io_context context{};
auto gen_future = asio::co_spawn(context, count_chars_test(), asio::use_future);
REQUIRE(gen_future.get() == 9);
}
< /code>
Использование C ++ 20 с G ++ 14.1 в Explorer компилятора я вижу следующий выход из компилятора: < /p>
не может связать неконфекс Lvalue reference of type 'boost :: basic_striew_view &' to an rval 'std :: rement_reference :: type' {aka 'boost :: basic_string_view Объявления, как я предоставил. 421 | std::exchange(awaited_from, noop_coroutine())};Как можно определить подпись для Asio :: Experimental :: coro , например, выше, чтобы получить строку какой-либо формы в count_chars_coro ? PrettyPrint-Override ">
Код: Выделить всё
In file included from /app/boost/include/boost/asio/experimental/coro.hpp:293,
from :4:
/app/boost/include/boost/asio/experimental/impl/coro.hpp: In instantiation of 'auto boost::asio::experimental::detail::coro_promise_exchange::yield_value(const Yield&) [with Yield = long unsigned int; Input = boost::basic_string_view; Return = long unsigned int]':
:14:24: required from 'boost::asio::experimental::coro count_chars_coro(Executor) [with Executor = boost::asio::any_io_executor; stype = boost::basic_string_view]'
14 | stype const next = co_yield n;
| ^~~~~~~~
:23:47: required from 'boost::asio::awaitable count_chars_test() [with Executor = boost::asio::any_io_executor; stype = boost::basic_string_view]'
23 | auto gen = count_chars_coro(co_await asio::this_coro::executor);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:32:84: required from here
32 | auto gen_future = asio::co_spawn(context, count_chars_test(), asio::use_future);
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/app/boost/include/boost/asio/experimental/impl/coro.hpp:421:54: error: cannot bind non-const lvalue reference of type 'boost::basic_string_view&' to an rvalue of type 'std::remove_reference::type' {aka 'boost::basic_string_view'}
421 | std::exchange(awaited_from, noop_coroutine())};
|
Код: Выделить всё
/app/boost/include/boost/asio/experimental/impl/coro.hpp:421:54: error: cannot bind non-const lvalue reference of type 'boost::basic_string_view&' to an rvalue of type 'std::remove_reference::type' {aka 'boost::basic_string_view'}
421 | std::exchange(awaited_from, noop_coroutine())};
|
Код: Выделить всё
[build] /usr/local/src/boost/boost_src/libs/asio/include/boost/asio/experimental/impl/coro.hpp:420:31: error: non-const lvalue reference to type 'shared_ptr' cannot bind to a temporary of type 'shared_ptr'Подробнее здесь: https://stackoverflow.com/questions/797 ... coroutines
Мобильная версия