Category : variant

I am working on a snippet of code which will automatically check the types contained inside of std::variants and call a function from it’s subclass accordingly with the types extracted. I worked out a somewhat limited version of what I’d like to do: #include <variant> #include <optional> #include <string> #include <vector> #include <type_traits> #include <stdexcept> ..

Read more

I wonder if I have a templated function foo(), template<typename TYPE> void foo (TYPEA a) { …; } can I call it with a std::variant Type like the following? Type = std::variant<uint16_t, uint32_t>; Type someNumber = VALUE; foo<Type>(someNumber); Or my understanding about templates and variants are completely wrong? Thank you in advance for the help! ..

Read more

I have the following code: #include <variant> #include <string> #include <iostream> using Variant = std::variant<double, std::string>; // helper type for the visitor template<class… Ts> struct overloaded : Ts… { using Ts::operator()…; }; // explicit deduction guide (not needed as of C++20) template<class… Ts> overloaded(Ts…) -> overloaded<Ts…>; std::string string_from(const Variant& v) { return std::visit(overloaded { [](const ..

Read more

I have figured out that std::visit can be used the following way: std::visit([](auto&& arg) { using T = std::decay_t<decltype(arg)>; if constexpr (std::is_same_v<T, int>) std::cout << "int with value " << arg << ‘n’; else if constexpr (std::is_same_v<T, std::string>) std::cout << "std::string with value " << std::quoted(arg) << ‘n’; else static_assert(always_false_v<T>, "non-exhaustive visitor!"); }, v); But ..

Read more

I am using C++ Builder 10.2 (Rad Studio 10.2.3) I had some issues and needed to use boost variant Since I integrated boost variant, I can no longer link my application. I am always getting an "out of memory" error message, both for 32 bit and 64 bit platforms. I tried the solution below: https://docwiki.embarcadero.com/RADStudio/Tokyo/en/Handling_Out_of_Memory_Errors. ..

Read more