I might be doing something obviously wrong, but why would this print garbage on MSVC?
#include <sdkddkver.h>
#include <boost/asio.hpp>
#include <iostream>
namespace asio = boost::asio;
template <typename Str>
asio::awaitable<void> coro_task(Str&& str) {
std::cout << "coro_task: " << str << std::endl;
co_await asio::this_coro::executor;
}
template <typename Str>
void sync_task(Str&& str) {
std::cout << "sync_task: " << str << std::endl;
}
int main() {
asio::io_context ioc;
asio::co_spawn(ioc.get_executor(), coro_task("hello world"), asio::detached);
asio::post(ioc.get_executor(), [] { sync_task("hello world"); });
ioc.run();
}
This gives for example:
coro_task: ╕÷ⁿäg
sync_task: hello world
I can't figure out why this is the case. Surely Str
would be a static-lifetime char[12] &
, so I can't figure out why its getting garbage data.
If I pass anything with a temporary lifetime to coro_task
(e.g. coro_task(std::string{"hello world"})
) I get no output (str
is empty). I assume it's being moved from somewhere, but don't know where.
This issue was encountered while trying to create a generic async task/corouitine wrapper for blocking functions: https://stackoverflow.com/a/75228866/3554391. This question represents a minimal example of the issue.
The issue does not appear to happen with gcc on godbolt or with clang.
I am using visual studio 17.5.5 and asio 1.25.0.