Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost::spirit program crashes
From: Lars Rohwedder (l.rohwedder_at_[hidden])
Date: 2010-03-26 09:43:36


> You have a dangling reference to a rule here:
>
> const rule<>& parser;
>
> when you do this:
>
> const Configurable cfg = { "Hardtest", +( __ ) };
>
> +( __ ) creates a temporary rule and you assign the address of that
> to cfg.parser.

I thought I bound the temporary to a const reference, so the temporary
lives as long as the reference lives, like in this example:

std::string& s = std::string("Foo") + "Bar";

> Remove the reference:
>
> const rule<> parser;
>
> Keep in mind though that "classic" spirit rules are not copyable.
> So don't go and copy your cfg around.

That's why I want to use references, to avoid copying rules around.

I solved the problem by writing

  // unchanged:
  struct Configurable { const char* name; const rule<>& rule; };

  // make an explicit object here, not a bound temporary:
  const rule<> myRule = +(__)>>(...);

  // bound the reference to the copy:
  const Configurable cfg = {"Name", myRule };

and it seems to work. :-)

Lars R.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net