Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
karma legacy "variant" with attr_cast
#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 spirit { namespace traits { template<> struct transform_attribute<const U,UVariant,ka::domain> { typedef UVariant type; static type pre(const U & u) { if(u.kind) { return type(u.foo); } else { return type(u.bar); } } }; }}} typedef std::back_insert_iterator<std::string> iterator; class grm: public ka::grammar<iterator, U()> { public: grm():grm::base_type(start) { start = ka::attr_cast< UVariant >(foo | bar); foo = ka::double_; bar = ka::uint_; } private: ka::rule<iterator,U()> start; ka::rule<iterator,double()> foo; ka::rule<iterator,size_t()> bar; }; int main(int argc, char * argv[]) { grm g; U u; u.kind = false; u.foo = 1.0; u.bar = 34; std::string generated; std::back_insert_iterator<std::string> sink(generated); ka::generate(sink,g,u); std::cout << generated << std::endl; return 0; }
run
|
edit
|
history
|
help
0
Template
#32
MSVC_example_GetAllocLength
VS struct name enumerate
Thing v.2
copy
multiply linked list numbers
virtual inheritance
GetFinalPathNameByHandle Behaviour
Problem_rstring_1