|
Boost : |
From: Eric Niebler (eric_at_[hidden])
Date: 2004-05-27 18:25:21
David Abrahams wrote:
> "Eric Niebler" <eric_at_[hidden]> writes:
>>Why is that confusing? That's how most other types in C++ behave:
>>
>>int a,b; // a and b are undefined
>>a = b; // a and b are still undefined
>
> ^^^^^
> This is undefined behavior, actually.
Oops, you're right. Bad example. How about:
int a=0,b=0;
a=b;
b=1; // b is 1, a is still 0
My point was that rule<> doesn't "do as the ints do", which can be
surprising to some. I would love for this to work:
rule<> a;
{
rule <> b;
// match balanced, nested parens.
// b refers to itself by reference
// (Note: ! means optional, no special syntax
// required to get by-ref semantics here)
b = '(' >> !b >> ')';
// a assumes b's behavior
a = b;
}
// a still has b's behavior.
// this matches balanced, nested parens
parse( "(())", a );
Currently, this crashes because 'a' holds 'b' by reference and tries to
access it after 'b' has gone out of scope and been destroyed. I haven't
yet thought of a way to make this work, but I'm not convinced it's
impossible.
I now agree that rules should have by-ref semantics when embedded in
another rule. But in simple assignment statements ("a=b;"), it would be
nice if rules behaved as the ints do. I'm sure this will draw fire from
the EBNF purists who want rule assignment to have by-ref semantics, but
IMO C++ types that don't have normal copy and assignment semantics are
just hard to work with.
-- Eric Niebler Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk