boost spirit V2 qi bug associated with optimization level

后端 未结 1 1124
醉酒成梦
醉酒成梦 2020-11-29 12:36

I develop my code in my spare time. Preferably in debug mode. Recently, when I tried to build release version, then I got the error (runtime, output: 1\\n2\\n t

相关标签:
1条回答
  • 2020-11-29 13:21

    It's a bug in your code, nothing wrong with the compiler or the optimization levels.

    The cinch is with expression templates (like the ones used by Boost Proto, and hence by Boost Spirit). They are only valid to the end of their enclosing full expression [1]

    The canonical workaound is:

     BOOST_SPIRIT_AUTO(ana, *~qi::char_('*') > +qi::char_('*'));
    

    You can find it here: http://svn.boost.org/svn/boost/trunk/libs/spirit/example/qi/typeof.cpp and it got first introduced in the comments at this blog post: http://boost-spirit.com/home/articles/qi-example/zero-to-60-mph-in-2-seconds/

    The explicit fix I tested (which worked nicely on my box, no warnings remaining in valgrind):

    auto const ana = boost::proto::deep_copy(
            *~qi::char_('*') > +qi::char_('*'))
            ;
    

    Spirit X3 promises to remove this wart. Slightly related, I think Protox11 also removes this issue by being aware of references at all times.


    [1] Grep the standard for lifetime extension of temporaries. The expression templates keep references to the literals used (the rest has value semantics anyways), but the temporaries aren't bound to (const) references. So they go out of scope. Undefined Behaviour results

    0 讨论(0)
提交回复
热议问题