std-variant

Throw exception on missing function overload with std::variant instead of compile time error

笑着哭i 提交于 2020-05-17 07:20:06
问题 This is a follow up to this question Consider the following code #include <variant> int add_(int a, int b){ return a+b; } float add_(float a, float b){ return a+b; } float add_(int a, float b){ return a+b; } float add_(float a, int b){ return a+b; } using Number = std::variant<int, float>; Number add(Number const& lhs, Number const& rhs ){ return std::visit( []( auto& lhs_, auto& rhs_ )->Number { return {add_( lhs_, rhs_ )}; }, lhs, rhs ); } int main(){ Number a = 1.f; Number b = 2.f; Number

In C++, how to make a variant that can contain a vector of of same variant?

守給你的承諾、 提交于 2020-01-03 10:54:23
问题 I a trying to make a std::variant that can contain a vector of the same variant: class ScriptParameter; using ScriptParameter = std::variant<bool, int, double, std::string, std::vector<ScriptParameter> >; I am getting ScriptParameter redefinition. It think it is possibly because a template parameter cannot be forward declared? Is there a way to achieve a variant that could also contain an array of same typed variants? 回答1: Since the forward declaration says ScriptParameter is a class, you can

boost::mpi and boost:serialization with std::variant

自古美人都是妖i 提交于 2019-12-24 16:48:05
问题 c++17 introduces the new type std::variant . Is it possible to define a serialization routine, so as to use std::variant in conjunction with boost::mpi ? Consider, e.g., a simple program #include <variant> #include <boost/mpi.hpp> #include <boost/serialization/string.hpp> namespace mpi = boost::mpi; class A { friend class boost::serialization::access; template <class Archive> void serialize(Archive& ar, const unsigned int version) { ar & x; ar & y; } public: int x, y; }; class B { friend

get currently held typeid of std::variant (like boost::variant type())

南笙酒味 提交于 2019-12-08 02:01:36
问题 I've migrated from boost::variant to std::variant, and hit a snag. I was using a nice function in boost 'type()' that lets you get the currently held typeid. See https://www.boost.org/doc/libs/1_48_0/doc/html/boost/variant.html#id1752388-bb How can this be achieved with std::variant? I have an unordered map key'd on 'type_index', which holds some value 'std::function'. My variant, depending on the type, will determine what function I grab from the map to do some operation. (The code I have is

get currently held typeid of std::variant (like boost::variant type())

不问归期 提交于 2019-12-06 09:37:43
I've migrated from boost::variant to std::variant, and hit a snag. I was using a nice function in boost 'type()' that lets you get the currently held typeid. See https://www.boost.org/doc/libs/1_48_0/doc/html/boost/variant.html#id1752388-bb How can this be achieved with std::variant? I have an unordered map key'd on 'type_index', which holds some value 'std::function'. My variant, depending on the type, will determine what function I grab from the map to do some operation. (The code I have is far too large to post). Any implementation ideas aside from writing a specific visitor for a

`std::variant` vs. inheritance vs. other ways (performance)

那年仲夏 提交于 2019-12-04 07:50:24
问题 I'm wondering about std::variant performance. When should I not use it? It seems like virtual functions are still much better than using std::visit which surprised me! In "A Tour of C++" Bjarne Stroustrup says this about pattern checking after explaining std::holds_alternatives and the overloaded methods: This is basically equivalent to a virtual function call, but potentially faster. As with all claims of performance, this ‘‘potentially faster’’ should be verified by measurements when

May I change the held type in a std::variant from within a call to std::visit

折月煮酒 提交于 2019-11-30 10:59:43
Does the following code invoke undefined behaviour? std::variant<A,B> v = ...; std::visit([&v](auto& e){ if constexpr (std::is_same_v<std::remove_reference_t<decltype(e)>,A>) e.some_modifying_operation_on_A(); else { int i = e.some_accessor_of_B(); v = some_function_returning_A(i); } }, v); In particular, when the variant does not contain an A , this code re-assigns an A while still holding a reference to the previously held object of type B . However, because the reference is not used anymore after the assignment, I feel the code is fine. However, would a standard-library be free to implement

May I change the held type in a std::variant from within a call to std::visit

我怕爱的太早我们不能终老 提交于 2019-11-29 16:24:56
问题 Does the following code invoke undefined behaviour? std::variant<A,B> v = ...; std::visit([&v](auto& e){ if constexpr (std::is_same_v<std::remove_reference_t<decltype(e)>,A>) e.some_modifying_operation_on_A(); else { int i = e.some_accessor_of_B(); v = some_function_returning_A(i); } }, v); In particular, when the variant does not contain an A , this code re-assigns an A while still holding a reference to the previously held object of type B . However, because the reference is not used