boost-variant

Why does boost::spirit::qi::parse() not set this boost::variant's value?

喜你入骨 提交于 2019-12-03 16:00:35
When trying to parse text into a boost::variant, the variant's value does not get changed. The parsers by themselves appear to work fine, so my assumption is that I'm doing something wrong with the variant code. I'm using boost 1.46.1 and the following code compiles in Visual Studio 2008. 1st Update hkaiser noted that the rule and grammar template arguments must not be Variant but Variant() . This got a bit "further" as I now have a compilation error in boost_1_46_1\boost\variant\variant.hpp(1304) . The comment says: // NOTE TO USER : // Compile error here indicates that the given type is not

How to achieve dynamic polymorphism (run-time call dispatch) on unrelated types?

孤者浪人 提交于 2019-12-02 19:24:26
GOAL: I would like to achieve type-safe dynamic polymorphism (i.e. run-time dispatch of a function call) on unrelated types - i.e. on types which do not have a common base class . It seems to me that this is achievable, or at least theoretically sound. I will try to define my problem more formally. PROBLEM DEFINITION: Given the following: two or more unrelated types A1, ..., An , each of which has a method called f , possibly with different signatures, but with the same return type R ; and a boost::variant<A1*, ..., An*> object v (or whatever other type of variant) which can and must assume at

Boost.Variant, Boost.MPL: How to append types?

烈酒焚心 提交于 2019-12-02 18:08:39
问题 I look at this grate code based on boost.Any and cant help but wonder if we could use Boost.Variant instead. I wonder if such API would be possible: void voidFunc() { std::cout << "void called" << std::endl; } int stringFunc(std::string str) { std::cout << str << std::endl; return 0; } int main() { some_map_like_type<std::string, boost::variant> funcs; funcs.insert<void , void >("voidFunc", &voidFunc)); // now our variant vould contain something like boost::function<void, void> funcs.insert

Boost.Any vs. Boost.Variant

試著忘記壹切 提交于 2019-12-02 17:23:57
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. drby 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

Boost.Variant, Boost.MPL: How to append types?

流过昼夜 提交于 2019-12-02 11:28:38
I look at this grate code based on boost.Any and cant help but wonder if we could use Boost.Variant instead. I wonder if such API would be possible: void voidFunc() { std::cout << "void called" << std::endl; } int stringFunc(std::string str) { std::cout << str << std::endl; return 0; } int main() { some_map_like_type<std::string, boost::variant> funcs; funcs.insert<void , void >("voidFunc", &voidFunc)); // now our variant vould contain something like boost::function<void, void> funcs.insert<int , std::string>("stringFunc", &stringFunc)); // and now we added to our variant a new type: boost:

boost::spirit::karma output of string in quotation marks

落花浮王杯 提交于 2019-12-02 02:21:42
问题 I am trying to escape a string in quotation marks using boost::spirit::karma. This works fine if it's just a string. However, for a string in a boost::variant in a std::vector, it does not. Just printing the string does work however, I do not quite understand why. Line (1) works fine, but doesn't do what I want. Line (2) should do it, but doesn't. #include <iostream> #include <string> #include <boost/variant.hpp> #include <boost/spirit/include/karma.hpp> namespace karma = boost::spirit::karma

boost::spirit::karma output of string in quotation marks

此生再无相见时 提交于 2019-12-02 02:18:45
I am trying to escape a string in quotation marks using boost::spirit::karma. This works fine if it's just a string. However, for a string in a boost::variant in a std::vector, it does not. Just printing the string does work however, I do not quite understand why. Line (1) works fine, but doesn't do what I want. Line (2) should do it, but doesn't. #include <iostream> #include <string> #include <boost/variant.hpp> #include <boost/spirit/include/karma.hpp> namespace karma = boost::spirit::karma; typedef std::vector<boost::variant<int, std::string>> ParameterList; typedef boost::variant<int, std:

Recursive using declaration with boost variant

孤街浪徒 提交于 2019-12-01 10:30:43
问题 I am trying to represent a tree-like recursive data structure where each node may be one of two different data types. I employ the boost variant to "house" the two types that might be present at each node. However, I run into a problem. I'm declaring all these types strictly with 'using' directives, so when I get to the recursive nature of a node, it fails since typedef/using may not use recursion. How to accomplish this? using LeafData = int; // just for illustration using LeafNode = std:

Why can't I visit this custom type with boost::variant?

雨燕双飞 提交于 2019-12-01 06:20:41
问题 The following code: #include <boost/variant.hpp> #include <iostream> #include <string> struct A { A() { } ~A() throw() { } A& operator=(A const & rhs) { return *this; } bool operator==(A const & rhs) { return true; } bool operator<(A const & rhs) { return false; } }; std::ostream & operator<<(std::ostream & os, A const & rhs) { os << "A"; return os; } typedef boost::variant<int, std::string, A> message_t; struct dispatcher_t : boost::static_visitor<> { template <typename T> void operator()(T

Boost Variant: how to get currently held type?

江枫思渺然 提交于 2019-11-30 04:41:04
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)> func1; typedef boost::variant<int, func0, func1> variant_func; func1 fn = std::plus<double>(); variant