Is required for class templates to be instantiated to use only pointer or reference to them?

When using just pointers/references to std::variant<T…> defined from T… where some of T is only forward declared – I have this problem that I cannot even use pointers nor references to this std::variant – because its bases classes wanted to be instantiated – and they require std::is_default_constructible and other traits. See example code: #include <variant>… Read More Is required for class templates to be instantiated to use only pointer or reference to them?

runtime indexing with std::get

I’m trying to turn a vector of variants into a tuple of vectors variadicly (i.e. I’d like to factor out the following code into a variadic template std::variant<Ts…>). std::vector<std::variant<float, int>> vector_of_variants; vector_of_variants.emplace_back(1); vector_of_variants.emplace_back(2.f); std::tuple<std::vector<float>, std::vector<int>> result; for (auto& el : vector_of_variants) { auto index = el.index(); std::get<index>(result).pushback(std::get<index>(el)); // error: index is runtime value } However,… Read More runtime indexing with std::get

Difference in supplying two variant objects separately vs in an array

I have two variant objects of the following type struct FigureMove {}; struct PieceMove {}; using Move = std::variant<FigureMove, PieceMove>; I want to supply two Move objects to a function and call different functions depending on the underlying types in the variants. I have two different versions of the function which takes the Move objects.… Read More Difference in supplying two variant objects separately vs in an array

Correct use of std::variant and std::visit when functor requires multiple arguments

I have been struggling a bit to get my code using std::variant in combination with std::visit to work. I have now reduced my code to a minimum of just using one type in my variant and still the compiler complains about no matching call to visit. #include <iostream> #include <variant> #include <vector> #include <array> class… Read More Correct use of std::variant and std::visit when functor requires multiple arguments

How to direct initialize a C++ variant with one of its non-default option?

Consider the following: class Foo { […] public: Foo(int); } class Bar { […] public: Bar(int); } using MyVariant = std::variant<Foo,Bar>; How can one direct initialize (i.e no copy and no Foo building involved) an instance of MyVariant as a Bar ? >Solution : Pass the a tag of type std::in_place_type_t<Bar> as first constructor parameter:… Read More How to direct initialize a C++ variant with one of its non-default option?

Why does operator==(std::variant<T, U>, T) not work?

Consider the following: #include <iostream> #include <variant> int main () { std::variant<int, float> foo = 3; if(foo == 3) { std::cout << "Equals 3\n"; } } Godbolt demo here This does not compile because of the foo == 3: <source>:7:12: error: no match for ‘operator==’ (operand types are ‘std::variant<int, float>’ and ‘int’) 7 | if(foo… Read More Why does operator==(std::variant<T, U>, T) not work?