boost-variant

Boost variant visitor with an extra parameter

烈酒焚心 提交于 2021-02-15 20:51:04
问题 I have code that resembles below. typedef uint32_t IntType; typedef IntType IntValue; typedef boost::variant<IntValue, std::string> MsgValue; MsgValue v; Instead of saying this, IntValue value = boost::apply_visitor(d_string_int_visitor(), v); I would like to pass an extra parameter like this: But operator() gives a compile error. //This gives an error since the overload below doesn't work. IntValue value = boost::apply_visitor(d_string_int_visitor(), v, anotherStr); class d_string_int

boost::variant apply static_visitor to certain types

一个人想着一个人 提交于 2021-01-28 07:12:22
问题 I have the following variant: typedef boost::variant<int, float, bool> TypeVariant; And I want to create a visitor to convert a int or float type to a bool type. struct ConvertToBool : public boost::static_visitor<TypeVariant> { TypeVariant operator()(int a) const { return (bool)a; } TypeVariant operator()(float a) const { return (bool)a; } }; However this is giving me the error message: 'TypeVariant ConvertToBool::operator ()(float) const': cannot convert argument 1 from 'T' to 'float' What

Assign variant<A,B,C> from variant<C,B>?

天涯浪子 提交于 2021-01-27 14:51:33
问题 Using = does not work. I have code like this, but it is a "bit" ugly. #include <iostream> #include <cassert> #include <variant> #include <string> using namespace std; namespace detail { template<typename... L, typename... R> void VariantAssignRec(variant<L...>* lhs, const variant<R...>&rhs, size_t rhs_idx, std::integral_constant<int, -1>) { } template<typename... L, typename... R, int get_idx> void VariantAssignRec(variant<L...>* lhs, const variant<R...>&rhs, size_t rhs_idx, std::integral

C++ Mutually Recursive Variant Type

断了今生、忘了曾经 提交于 2020-01-14 10:48:29
问题 I am trying to represent a PDF object type in c++ using variants. A PDF object is one of the following: Boolean Integer Real String Name Stream Array<Object> Map<Object, Object> As you can see, the Object type is mutually recursive because the Array type would require a declaration of the Map type which would require a declaration of the Array type. How could I go abouts representing this type in c++? If a variant isn't the best way, what is? Here is what I have tried so far but it doesn't

How to get around matching Boost Variant return types?

时光毁灭记忆、已成空白 提交于 2020-01-06 21:20:55
问题 Linked Question Suppose we have: boost::variant<nil, std::string, string_struct> And, struct string_struct { string_struct(std::string const& name = "") : name(name) {} std::string name; } Is there some way to prevent the compiler from incorrectly inferring the wrong boost variant type? More theoretically, if we had a boost::variant<std::string, std::string> I don't think it would compile. The first example would, however. The problem is, how do we guarantee no collision of return types?

How to generalize a tree structure with variant/visitor

天大地大妈咪最大 提交于 2020-01-01 12:09:08
问题 This is a part 2 of my question, originally posted here. Thanks to @sehe for clarifications and help. I ended up with the code that follows, but I can't figure out how can I reduce this thing to a generic solution with variant and visitor. Help/advise is greatly appreciated. Thanks. #include "stdafx.h" #include <iostream> #include <memory> #include <string> #include <vector> #include <boost/format.hpp> #include <boost/variant.hpp> template <typename T> class A { public: typename T L; typename

Boost.Any vs. Boost.Variant

强颜欢笑 提交于 2019-12-31 08:41:09
问题 I'm having trouble choosing between Boost.Any and Boost.Variant. When should I use each one? What are the advantages and disadvantages of each? I am basically looking to store some states from external sources. 回答1: Have you looked at the comparison in the variant library already? (Not sure what states from external sources are, so it's kind of hard to say what's more appropriate for you.) 来源: https://stackoverflow.com/questions/1366524/boost-any-vs-boost-variant

Generating a vector with int and string arguments

让人想犯罪 __ 提交于 2019-12-25 18:29:36
问题 I would like to use the boost library (boost::variant) in C++ to define a vector if integers and strings. I am struggling to fill a such a vector - can someone either post an example code with fills a vector with ints and strings using the Boost library and reads elements of the vector or otherwise direct me to an example. I searched for articles with the tage boost::variants on the SO, but could not find what I wanted. 回答1: Here are some examples (written from memory): typedef boost::variant

Generating a vector with int and string arguments

无人久伴 提交于 2019-12-25 18:29:09
问题 I would like to use the boost library (boost::variant) in C++ to define a vector if integers and strings. I am struggling to fill a such a vector - can someone either post an example code with fills a vector with ints and strings using the Boost library and reads elements of the vector or otherwise direct me to an example. I searched for articles with the tage boost::variants on the SO, but could not find what I wanted. 回答1: Here are some examples (written from memory): typedef boost::variant

Boost::Variant and function_types in it: How to put functions into Boost::variant?

北战南征 提交于 2019-12-24 15:00:23
问题 Lyrics: I try to implement a task pool over MPI. So I need some kind of RPC but one that would work between different parts of my program, meaning processor A wants processor B to call function C with argument D. We can not pass pointers to functions between processes like we do with threads, so we need some wrapper container to hold our function pointers at each process instance. All inside one source file\one program... So I started wondering about How to store functional objects with