Re: x3::variant vs boost::variant
Baptiste Wicht <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <[email protected]> |
On Mon, Feb 08, 2016 at 07:53:14AM +0800, Joel de Guzman wrote:
> On 1/30/16 5:24 PM, Baptiste Wicht wrote:
> > Hi guys,
> >
> > I just tested compiling my X3 parser with Boost 1.59 and it worked like
> > a charm :)
> >
> > Now, I'll start using my real AST instead of the nodes I've written to
> > test X3.
> >
> > I'm having a problem at this point with variant types. My old AST use
> > boost::variant and recursive_wrapper while my new AST use x3::variant
> > and forward_ast.
> >
> > The new way (especially getting rid of recursive_wrapper) is nicer in my
> > opinion, so I tried adapting my tree to use x3::variant and forward_ast,
> > but it seems that a lot of code will need to be changed :( I found at
> > least issues with operator<< (easily fixable). And apparently you cannot
> > construct a x3::variant<x1,x2> from a x2. That seems really extreme
> > since I'm doing that all over the place with boost::variant.
> >
> > It seems that X3 don't mind boost::variant and recursive_wrapper, so
> > what would be the advantages of using x3::variant and forward_ast ?
>
> There are performance advantages with x3::variant. Boost variant has this
> odd (IMO) requirement that prevents it to move recursive variants
> cheaply. There was an ensuing debate over that some years ago. It
> boils down to the never-empty guarantee. The way around that is to
> have some kind of nullable object that I use in x3 to make move a
> cheap pointer swap.
That is what I thought. Thanks for the answer.
> Another advantage is that you have a unique type everytime. An xr variant
> is a user struct/class that you can use for overloading and type detection.
> A variant, OTOH is defined by its structure. Hence, for example, variant<float, int>
> does not say about its type, but only its structure. If you have a typedef
> my_type and his_type both having the same variant<float, int>, they are not
> unique types.
>
> Patches/PRs very welcome of course.
I've had some time to investigate the issue again:
* I had to use explicit construction in several places
* I had to add operators for x3 variants
The main problem that remains is with x3::variant and x3::forward_ast:
I have a type like this:
using Type = boost::spirit::x3::variant<
SimpleType,
boost::spirit::x3::forward_ast<ArrayType>,
boost::spirit::x3::forward_ast<TemplateType>,
boost::spirit::x3::forward_ast<PointerType>
>;
And a lot of code resembling this:
if(auto* ptr = boost::get<ast::SimpleType>(&type)){
//Do something on *ptr
} else if(auto* ptr = boost::get<ast::ArrayType>(&type)){
//Do something on *ptr
} else if(auto* ptr = boost::get<ast::PointerType>(&type)){
//...
}
//...
But this does not work since ArrayType is not a member of the variant
because of the forward_ast and I have an error from boost strict_get.
I really have a lot of code like this, I won't be able (neither have the
motivation for it) to change all of it. If there is no simple solution,
I'll use my old AST with boost::variant and recursive_wrapper.
Is there anything I can do to use boost::get with forward_ast ?
Thanks
Baptiste
------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Spirit-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/spirit-general
signature.asc
(application/pgp-signature, 801 B)
-----BEGIN PGP SIGNATURE----- iQIcBAEBCgAGBQJWxNyjAAoJEMVWa2x/iEUy2KEP/0mE5SPfKGiHFvoiN/a1UQz0 zFDHCBv4/OxN+24q9v7H9W3kTeXJMtrcCpyLQfHIsuMjIjMVUvdl2wyP9POk6F76 TLpoqlH7dutu15iIkdIsVh5yufZnItZ204ldsdcm1ffUPkojtxmCprblsx6FGJIS MXxIHIFsiJRKei4t4XLxkVs6B04H/OqL4Y69pTzssKGCAo28te9/TmFhrD2k0GkD SWjzsqkNCJwtcWzxcupuzNusMms7d20oOVLrTPwDAqMUqi2IR1izu6e3aun2NH59 p/52Ue5nryOmlsOq/x2PkdTKQNtysVpVOBrAljntzlGA+EERWEkGuQBdGGNFF+Kp 1nFVxYoXgcv9IoEhvo6x21vKASOIg8QTt06RGXX+TrpwfZ2i3tG/i13t54b5Kjt6 av4TJI/XVLVFH9bH0Iq4UegRu602x/L5qCZW448WWOq2xnQueVZtoG7Nsd4tE6Ll ZnMnABxGwfFVqn77MV2SDNOLt1quHfCw9ZFrnws7xXLvR+FoCgaYYFDorj41jY7R POdZV5qkWkLrYU+hk13REczYXNKqJitwTQURutCoZDVByq9waB2lwVR6H0Wvwkzg IpsNJjFrIxXasvHf+IyQAC7i3zWhrufqNTKISo8Pa5tMOfsuF/Pc7+wc043I+KZx S0mAgk9PJeIUfHGdKalM =nLyx -----END PGP SIGNATURE-----