小编ma-*_*nln的帖子

传递指针参数时意外的模板实例化

以下代码无法编译(在 godbolt.org 上使用带有 /std:c++latest 的 MSVC 19.28 和带有 -std=c++20 的 Clang 12.0 进行测试):

#include <variant>

struct some_thing;
using var = std::variant<some_thing>;

// There should be NO need to instantiate the template here, right?
void take(var* v) {}

int main() {
    var* v = nullptr;
    take(v); // This is the offending line - tries to instantiate the variant template and fails as 'some_thing' is undefined up until now.
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

对 main 的调用如何需要模板实例化?我们只是传递一个指针。这是一个错误还是我错过了什么?

模板实例,如果有一个指针类型的模板的工作(如我做的话),而不是时,不应发生typedef'ing它的typedef不引入新的类型(即模板实例)。我可能仍然缺少一些东西,但这应该使我的示例代码能够编译。

请参阅下面的 MSVC 错误消息。请注意,此问题与理解错误消息 …

c++ templates compiler-errors forward-declaration

5
推荐指数
1
解决办法
94
查看次数

ASIO C++ 协程取消

我正在生成一个协程,如下所示。

asio::co_spawn(executor, my_coro(), asio::detached);
Run Code Online (Sandbox Code Playgroud)

我该怎么取消它?

据我所知,每个处理程序取消只需通过将处理程序与asio::bind_cancellation_slot. 这在我的具体示例中不起作用(无论使用 asio::detached “处理程序”或某些 lambda),并且对我来说它不起作用是有道理的。

我发现这篇博文基本上说,从另一个协程中生成协程并等待它(通过适当的asio::bind_cancellation_slot设置)就可以解决问题。这对我来说失败了,因为my_coro在下面的示例中似乎从未运行过。此外,我什至不知道为什么这应该起作用(关于取消)。

asio::awaitable<void> cancelable(asio::cancellation_signal& sig, asio::awaitable<void>&& awaitable) {
    co_await asio::co_spawn(co_await asio::this_coro::executor, std::move(awaitable), asio::bind_cancellation_slot(sig.slot(), asio::use_awaitable));
    co_return;
}

// ...

asio::cancellation_signal signal;
asio::co_spawn(executor, cancelable(signal, my_coro()), asio::detached);
Run Code Online (Sandbox Code Playgroud)

asio::cancellation_signal在 ASIO 中将 a 连接到asio::cancellation_state协程(通过 获得co_await asio::this_coro::cancellation_state)的正确方法是什么?

如果有帮助的话:我在最新的 master 上使用 ASIO 独立版,即不是 boost 版本。

c++ boost-asio

5
推荐指数
1
解决办法
2066
查看次数