Re: copy constructor of symbols does not do a deep copy?

Seth via Spirit-general <[email protected]> Thu, 06 Apr 2023 16:02:29 +0200
Newsgroups gmane.comp.parsers.spirit.general
Message-ID <[email protected]>
It's by design, likely to make it efficient and easy (without surprises!) to use symbols in parser expressions. In effect, they get "reference semantics" just like `qi::rule<>` does when embedded in other rules.

You can easily deep-clone a symbol table if you need:

    template <typename Sym> Sym clone_syms(Sym const& i) {
        Sym o;
        i.for_each(o.add);
        return o;
    }

Here's a live demo driving home the points with more examples: http://coliru.stacked-crooked.com/a/263d22bc47d63375

On Mon, Apr 3, 2023, at 10:50 PM, James Walker wrote:
> If I run code like this
>
>     boost::spirit::qi::symbols<char, double> syms1;
>     boost::spirit::qi::symbols<char, double> syms2( syms1 );
>     syms1.at("x") = 0.0;
>     syms2.at("x") = 99.5;
>     std::cout << "In syms1, x=" << syms1.at("x") <<
>         " while in syms2, x=" << syms2.at("x") << std::endl;
>
> I get the output "In syms1, x=99.5 while in syms2, x=99.5".  Why didn't 
> x stay at 0.0 in syms1?
>
> _______________________________________________
> Spirit-general mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/spirit-general