Here's my code:
#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)
I'm getting the error no matching function for call to 'get'
but I don't figure out why. std::variant::index()
is constexpr, so it isn't the problem (I tested by putting directly the value 1
, but still the same error).
I have a std::monostate
to prevent an empty variant (when no args are in typename... Args
)