变体:没有用于调用 'get' 的匹配函数

Variant: no matching function for call to 'get'

这是我的代码:

#include <iostream>
#include <variant>
#include <vector>

class A {
public:
    virtual void Foo() = 0;    
};

class B : public A {
public:
    void Foo() override {
        std::cout << "B::Foo()" << std::endl;   
    }
};

class C :public A {
public:
    void Foo() override {
        std::cout << "C::Foo()" << std::endl;   
    }
};

template<typename... Args>
class D {
public:
    template<typename T>
    void Foo() {
        m_variant = T{};
    }

    void Bar() {
      std::get<m_variant.index()>(m_variant).Foo();
    }

private:
    std::variant<std::monostate, Args...> m_variant;
};

int main() {
    D<B, C> d;
    d.Foo<B>();
    d.Bar();

    d.Foo<C>();
    d.Bar();
}

(c.f wandbox.org)

我收到错误 no matching function for call to 'get',但我不明白为什么。 std::variant::index() 是 constexpr,所以这不是问题(我通过直接输入值 1 进行测试,但仍然是同样的错误)。
我有一个 std::monostate 来防止空变体(当 typename... Args 中没有参数时)

标记为constexpr的内容告诉您在某些情况下可以在编译时调用它。它保证它总是可以在编译时调用。

variant 的情况下,index 可以在编译时调用 如果变体本身是一个 constexpr 值 。否则它是一个运行时方法。

您可以阅读有关何时可以在编译时调用某些内容的文档,或者对其进行推理;在这种情况下,如果变体的类型在运行时可能会发生变化,那么 index 怎么可能是编译时间常量呢?请记住,只有值的 ttpe 和 constexpr ness,加上它本身是否在编译时上下文中,才能用于推断 "can this be called at compile time".

m_variant.index() 是运行时值(因为 m_variant 不是常量表达式)。

派发方式是使用visitor,如:

std::visit([](auto& v) { v.Foo(); }, m_variant);

Demo