boost-variant

Should this code fail to compile in C++17?

两盒软妹~` 提交于 2019-12-18 13:52:15
问题 I was updating a project to use C++17 and found a few instances where code that followed this pattern was causing a compile error on recent versions of clang: #include <boost/variant.hpp> struct vis : public boost::static_visitor<void> { void operator()(int) const { } }; int main() { boost::variant<int> v = 0; boost::apply_visitor(vis{}, v); } Using clang v8.0 in C++17 mode, this fails with the following error: <source>:11:30: error: temporary of type 'boost::static_visitor<void>' has

Is it safe to serialize a raw boost::variant?

╄→尐↘猪︶ㄣ 提交于 2019-12-17 19:44:48
问题 boost::variant claims that it is a value type. Does this mean that it's safe to simply write out the raw representation of a boost::variant and load it back later, as long as it only contains POD types? Assume that it will be reloaded by code compiled by the same compiler, and same version of boost, on the same architecture. Also, (probably) equivalently, can boost::variant be used in shared memory? 回答1: Regarding serialisation: It should work, yes. But why don't you use boost::variant 's

Is-braces-constructible type trait

孤者浪人 提交于 2019-12-17 14:56:29
问题 How can I check whether specific type typename T is constructible from arguments typename ...Args in the manner T{Args...} ? I aware of std::is_constructible< T, Args... > type trait from <type_traits> , but it works with parentheses, not curly braces. I do not have too much experience in writing of type traits, so I cannot provide initial example. As simplification we can accept any reasonable assertions, even if this leads to not too significant loss of generality. 回答1: template<class T,

casting attribute to boost::variant

廉价感情. 提交于 2019-12-13 02:40:21
问题 While learning how to use boost spirit, phoenix and fusion libraries, I came accross this minimal example that does not compile on msvc (2015, version 14) and boost 1.61.0 #include <boost/spirit/include/karma.hpp> #include <boost/variant/variant.hpp> namespace ka = boost::spirit::karma; struct U /* a kind of union (legacy code)*/ { bool kind; double foo; /* if kind = true */ size_t bar; /* if kind = false */ }; typedef boost::variant<double, size_t> UVariant; namespace boost { namespace

Boost Variant: how to get currently held type?

醉酒当歌 提交于 2019-12-12 07:11:20
问题 As I understood all types of boost.variant are parsed into real types (meaning as if boost variant<int, string> a; a="bla-bla" would after compilation turn into string a; a="bla-bla" ) And so I wonder: how to get what type was put into boost variant? What have I tried: #include <boost/variant.hpp> #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <iostream> int main() { typedef boost::function<double (double x)> func0; typedef boost::function<double (double x, double y)>

How can I use the boost visitor concept with a class containing state variables?

偶尔善良 提交于 2019-12-11 09:29:56
问题 I'm attempting to use boost::static_visitor to implement actions on a boost::variant type that affect the state of some variable. My approach was to contain all of the state variables in my command visitor class, but it seems this is not possible. Here is my code example: #include <string> #include <sstream> #include <vector> #include <boost/variant.hpp> #include <boost/foreach.hpp> struct TypeA { int varA; int varB; }; struct TypeB { std::string varA; std::string varB; }; typedef boost:

boost::get vs boost::apply_visitor when fetching values from a variant

这一生的挚爱 提交于 2019-12-11 08:57:36
问题 We are using collections of boost::variant extensively in our production code. The way we extract the values from this collection is for (auto & var : vars) { switch (var.which()) { case 1 : AVal = boost::get<A>(var); break; case 2 : BVal = boost::get<B> (var); ... } } Reading more about variants I can see that a different alternative would be for (auto & var : vars) { switch (var.which()) { case 1 : AVal = boost::apply_visitor(AVisitor, var); break; case 2 : BVal = boost::apply_visitor

How operator<< with boost::variant is implemented

时光毁灭记忆、已成空白 提交于 2019-12-10 16:09:44
问题 I understand that boost::variant is implemented something like so template <typename... Vs> struct variant { std::aligned_union<Vs...>::type buffer; .... }; How can we make an operator<< for a struct like this that prints the casts the type stored in the buffer and passes that to operator<< for cout ? For this we would need to know the type of the element stored in the buffer right? Is there a way to know this? Also I am looking for an explanation of such an implementation, if one exists. Not

boost::variant single storage guarantee

こ雲淡風輕ζ 提交于 2019-12-10 14:45:28
问题 My goal is to guarantee single storage on all my variant types: according to 'never empty' guarantee from Boost::variant, we need to override boost::has_nothrow_copy for each bounded type. But a bit later the documentation mentions something about 'boost::blank' and if that type is bound, variant will set that value rather than try to nothrow default copy constructors. what is not clear is if adding boost::blank in the bounded type list will avoid the requirement of overriding/specializing

boost variant simple call to common methods

时间秒杀一切 提交于 2019-12-10 03:31:38
问题 I have two pointers that only one of them can be set, so I am considering using boost::variant, say: boost::variant<shared_ptr<Type1> shared_ptr<Type2>> . Type 1 and 2 are different but they share some functionality. Thay for example, both have the method IsUnique . If I have the code to check the initialization: ASSERT(type1 != nullptr || type2 != nullptr); ASSERT(type1 == nullptr || type2 == nullptr); ASSERT(type1 == nullptr || type1->IsUnique()); ASSERT(type2 == nullptr || type2->IsUnique(