{"id":394,"date":"2009-11-17T05:52:50","date_gmt":"2009-11-17T13:52:50","guid":{"rendered":"http:\/\/boost-spirit.com\/home\/?page_id=394"},"modified":"2010-01-23T18:53:43","modified_gmt":"2010-01-24T02:53:43","slug":"zero-to-60-mph-in-2-seconds","status":"publish","type":"page","link":"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/","title":{"rendered":"Zero to 60 MPH in 2 seconds!"},"content":{"rendered":"<p>Wanna make a blazingly fast rule? Use <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_40_0\/doc\/html\/typeof\/refe.html\">BOOST_AUTO<\/a>! <a href=\"http:\/\/svn.boost.org\/svn\/boost\/trunk\/libs\/spirit\/example\/qi\/typeof.cpp\">Check it out here<\/a>. The code defines a named rule using C++0x auto when available and falls back to some template wizardry on older compilers. This code:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nBOOST_AUTO(comment, &quot;\/*&quot; &gt;&gt; *(char_ - &quot;*\/&quot;) &gt;&gt; &quot;*\/&quot;);  \r\n<\/pre>\n<p>defines a rule named comment that can be used just like any parser. If you have a compiler that can handle the new auto keyword, this code is equivalent to:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nauto comment = &quot;\/*&quot; &gt;&gt; *(char_ - &quot;*\/&quot;) &gt;&gt; &quot;*\/&quot;;  \r\n<\/pre>\n<p>Now you can use this `comment` rule to parse:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nbool r = parse(iter, end, comment);\r\n<\/pre>\n<p>Unlike Spirit rules, however, this one is a lot more efficient in terms of speed (no indirection), code size (virtually zero code bloat) and memory usage (the size of the comment rule above is just 8 bytes on MSVC. Just the footprint needed for the static strings, almost. The expression has zero overhead thanks to <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_37_0\/doc\/html\/proto.html\">Boost.Proto<\/a>). It is very suitable for skip-parsers, for example (if you want to know the actual type, you can use <a href=\"http:\/\/www.boost.org\/doc\/libs\/1_40_0\/doc\/html\/typeof\/refe.html\">BOOST_TYPEOF<\/a>). Be aware though that this is only good for non-recursive rules. There is no way to define recursive rules using this. But then, there are lots of rules that really aren&#8217;t recursive anyway. For example, we often need to break overly complex rules into manageable parts.So&#8230; there ya go&#8230; have fun!<\/p>\n<p>(UPDATE: See the comments below. It turns out that MSVC segfaults. Provided below is a workaround)<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>Share<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-394\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-394\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-394\" class=\"share-pinterest sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span>Pinterest<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-394\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\" ><span>Reddit<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span>Tumblr<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Wanna make a blazingly fast rule? Use BOOST_AUTO! Check it out here. The code defines a named rule using C++0x auto when available and falls back to some template wizardry on older compilers. This code: defines a rule named comment that can be used just like any parser. If you have a compiler that can [&hellip;]<\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon-text sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li><a href=\"#\" class=\"sharing-anchor sd-button share-more\"><span>Share<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><div class=\"sharing-hidden\"><div class=\"inner\" style=\"display: none;\"><ul><li class=\"share-facebook\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-facebook-394\" class=\"share-facebook sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=facebook\" target=\"_blank\" title=\"Click to share on Facebook\" ><span>Facebook<\/span><\/a><\/li><li class=\"share-twitter\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-twitter-394\" class=\"share-twitter sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\" ><span>Twitter<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-pinterest\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-pinterest-394\" class=\"share-pinterest sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=pinterest\" target=\"_blank\" title=\"Click to share on Pinterest\" ><span>Pinterest<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"sharing-linkedin-394\" class=\"share-linkedin sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\" ><span>LinkedIn<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-reddit\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-reddit sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=reddit\" target=\"_blank\" title=\"Click to share on Reddit\" ><span>Reddit<\/span><\/a><\/li><li class=\"share-tumblr\"><a rel=\"nofollow noopener noreferrer\" data-shared=\"\" class=\"share-tumblr sd-button share-icon\" href=\"http:\/\/boost-spirit.com\/home\/articles\/qi-example\/zero-to-60-mph-in-2-seconds\/?share=tumblr\" target=\"_blank\" title=\"Click to share on Tumblr\" ><span>Tumblr<\/span><\/a><\/li><li class=\"share-end\"><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><\/div><\/div>","protected":false},"author":2,"featured_media":0,"parent":384,"menu_order":3,"comment_status":"open","ping_status":"open","template":"article-page.php","meta":{"_s2mail":"","spay_email":""},"jetpack_shortlink":"https:\/\/wp.me\/PIHdZ-6m","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages\/394"}],"collection":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/comments?post=394"}],"version-history":[{"count":5,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages\/394\/revisions"}],"predecessor-version":[{"id":470,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages\/394\/revisions\/470"}],"up":[{"embeddable":true,"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/pages\/384"}],"wp:attachment":[{"href":"http:\/\/boost-spirit.com\/home\/wp-json\/wp\/v2\/media?parent=394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}