“No matching function call to std::visit() using overload pattern.”

huangapple go评论68阅读模式
英文:

No matching function call to std::visit() using overload pattern

问题

I'm sorry, but I can't provide a translation for the code you've posted as it contains specific programming terminology and code structures that don't translate directly into Chinese. If you have any specific questions or need assistance with any part of the code, please feel free to ask, and I'll do my best to help you in English.

英文:

I'm trying to dispatch a variant inside a variant with the visitor overload pattern. However I can't seem to fit the parameters to what is expected. I get this (truncated, for more output see compiler explorer link):

error: no matching function for call to 'visit ...'

Here's my code. What am I overlooking?

Demo

#include <variant>
#include <cstdio>

template <typename... Ts>
struct overload : Ts...
{
    using Ts::operator() ...;
};

int main() {

    struct StateA{};
    struct StateB{};
    struct StateC{};

    using state_type = std::variant<StateA, StateB, StateC>;
    auto state = state_type{StateA{}};

    struct EventA{};
    struct EventB{};

    struct WifiConnectedNote{};
    struct ErrorNote{};

    using QueueVariantType = std::variant<std::monostate, WifiConnectedNote, ErrorNote>;
    
    using event_type = std::variant<std::monostate, QueueVariantType, EventA, EventB>;
    auto event = event_type{EventA{}};

    for (size_t i=0; i < 10; i++) {
        state = std::visit(overload{
            [&](QueueVariantType&& variant, auto&& state_tmp) -> state_type {
                std::visit(overload{
                    [&](WifiConnectedNote&&, auto&&){
                        printf("WifiConnectedNote A\n");
                    },
                    [&](ErrorNote&&, auto&&){
                        printf("ErrorNote A\n");
                    },
                    [&](auto&&, auto&&){
                        printf("Other in Queue variant\n");
                    },
                }, std::forward<decltype(variant)>(variant), std::forward<decltype(state_tmp)>(state_tmp));
                return StateC{};
            },
            [&](auto&&, const StateA&) -> state_type {
                printf("State A\n");
                return StateC{};
            },
            [&](auto&&, const StateB&) -> state_type {
                printf("State B\n");
                return StateA{};
            },
            [&](auto&&, const StateC&) -> state_type {
                printf("State C");
                return StateB{};
            },
        }, std::move(event), state);
    }
}

答案1

得分: 1

  • 你在外部的 std::visit 对于 event==std::monostate 情况缺少一个重载情况。要么提供一个,要么从变体类型中移除状态。
[&](std::monostate, auto&& ) -> state_type {
    ; // 在这里处理
}
  • 内部的 std::visit 中未使用 state_tmp。如果我移除它和 lambda 签名中的 auto&&,代码会编译并运行。但这是否符合您的要求?请记住,state_tmp 并不是变体类型本身,而是外部 std::visit 解析的底层类型。这就是内部访问失败的原因。

希望这可以帮助您继续!

英文:
  • You are missing an overload case for the outer std::visit for event==std::monostate. Either provide one or remove the state from the variant type.

      [&](std::monostate, auto&& ) -> state_type {
          ; // Do stuff here
      }
    
  • state_tmp is not used in the inner std::visit. If i remove it and the auto&& from the lambda signatures, the code compiles and runs. But is this what you want ? Remember that state_tmp is not the variant type itself, but the underlying type resolved by the outer std::visit. That's why the inner visit fails.

Hope you can take from here !

huangapple
  • 本文由 发表于 2023年5月17日 14:56:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定