Category : variant

I have the following variant std::variant<Type1, Type2<subType1>> myVariant; Type2 is a template class, there are 2 possible sub types subType1, and subType2 are possible. Is the following supported? std::variant<Type1, Type2<std::variant<subType1,subType2>> myVariant; If yes, how would get<index>(myVariant) work with the inner variant? edit: fix typo Source: Windows Que..

Read more

I have following piece of code: #include <iostream> #include <string> #include <map> #include <variant> using namespace std; template <class… Ts> struct overloaded : Ts… { using Ts::operator()…; }; template <class… Ts> overloaded(Ts…)->overloaded<Ts…>; using element_dict = std::map<string, string>; using element_type = std::variant<string, element_dict, int>; void foo(element_type elements) { std::visit(overloaded{ [](string element) { cout << "stringn"; }, ..

Read more

Consider the following code: #include <variant> #include <cassert> struct foo { foo() noexcept; foo(const foo&) noexcept = default; foo(foo&&) noexcept = default; foo& operator=(const foo&) noexcept = default; foo& operator=(foo&&) noexcept = default; }; std::variant<std::monostate, foo> var; foo::foo() noexcept { assert(!var.valueless_by_exception()); }; int main() { var.emplace<foo>(); } With libstdc++ (from GCC 11), this works, but ..

Read more

I have a struct that contains a variant. I want to write a member function for that struct that should run code depending on which type variant currently holds. However, I have issues making it compile. I don’t want to use more "template shenanigans" like using a separate struct to define operator(T&) since it pollutes ..

Read more

I have following piece of code, but not sure if there are no copies here: std::variant<string, map<…>> words; … string text; text = std::visit( overloaded{ [](string words) { return words; }, [](map<…> words) { return make_text_from_dict(words); } }, std::move(words)); I’ve read description and haven’t seen if any of given options say that copy-elision is applied ..

Read more

I met a wired problem that my user-defined-constructor can only work on a non-array std::variant. Here is the code: template<size_t N> struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } char value[N]; }; template<StringLiteral _name, typename… ArgTys> struct Message { static_assert(sizeof(_name.value) <= 11U, "Name of message must less than 11 characters."); using ..

Read more

I met a wired problem that my user-defined-constructor can only work on a non-array std::variant. Here is the code: template<size_t N> struct StringLiteral { constexpr StringLiteral(const char (&str)[N]) { std::copy_n(str, N, value); } char value[N]; }; template<StringLiteral _name, typename… ArgTys> struct Message { static_assert(sizeof(_name.value) <= 11U, "Name of message must less than 11 characters."); using ..

Read more

I am using C++ Builder 10.2.3 (Rad Studio 10.2.3) I am trying to convert a TRectF to a Variant. But it seems not to be working: 1st Solution: using TValue::From(rect).AsVariant() or AsType(). I am getting the error message: Invalid Class TypeCast 2e Solution: Using TValue::Make() with ExtractRawData. But I cannot use it because __delphirtti() cannot ..

Read more