Discussion:
Compound assignment operator - is it described correctly?
(too old to reply)
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-10-12 22:51:02 UTC
Permalink
In the C++ Standard there is written according to the compound operator ( *8.5.18
Assignment and compound assignment operators, p.#7):*

7 The behavior of an expression of the form E1 *op *= E2 is equivalent to E1
= E1 *op *E2 except that E1 is
evaluated only once.
However I doubt whether this phrase is correct.
Consider the following code snippet

s = s + strlen( s ) - 1;

where s has type char *. If s is a pointer to an empty string then the
statement is equivalent to

s = s - 1;

However if to rewrite the statement like

s += strlen( s ) - 1;

Then the statement is equivalent to

s = s + std::numeric_limits<size_t>::max();


So it looks like E1 op = E2 is not equivalent to E1 = E1 op E2.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Nicolas Lesser
2018-10-12 23:00:04 UTC
Permalink
What you're missing is how the plus operator groups.

On Sat, Oct 13, 2018, 12:51 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
In the C++ Standard there is written according to the compound operator ( *8.5.18
Assignment and compound assignment operators, p.#7):*
7 The behavior of an expression of the form E1 *op *= E2 is equivalent to E1
= E1 *op *E2 except that E1 is
evaluated only once.
However I doubt whether this phrase is correct.
Consider the following code snippet
s = s + strlen( s ) - 1;
where s has type char *. If s is a pointer to an empty string then the
statement is equivalent to
s = s - 1;
However if to rewrite the statement like
s += strlen( s ) - 1;
This is not equivalent to the above, as you changed the grouping order.
The above is rewritten as:

s = s + (strlen(s) - 1);

Which suffers from the same problem.

The simple assignment above does not have an equivalent compound assignment
version.

Then the statement is equivalent to
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
s = s + std::numeric_limits<size_t>::max();
So it looks like E1 op = E2 is not equivalent to E1 = E1 op E2.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
'Vlad from Moscow' via ISO C++ Standard - Discussion
2018-10-12 23:10:08 UTC
Permalink
Could you elaborate>

суббПта, 13 Пктября 2018 г., 3:00:16 UTC+4 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
What you're missing is how the plus operator groups.
On Sat, Oct 13, 2018, 12:51 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
In the C++ Standard there is written according to the compound operator ( *8.5.18
Assignment and compound assignment operators, p.#7):*
7 The behavior of an expression of the form E1 *op *= E2 is equivalent
to E1 = E1 *op *E2 except that E1 is
evaluated only once.
However I doubt whether this phrase is correct.
Consider the following code snippet
s = s + strlen( s ) - 1;
where s has type char *. If s is a pointer to an empty string then the
statement is equivalent to
s = s - 1;
However if to rewrite the statement like
s += strlen( s ) - 1;
This is not equivalent to the above, as you changed the grouping order.
s = s + (strlen(s) - 1);
Which suffers from the same problem.
The simple assignment above does not have an equivalent compound
assignment version.
Then the statement is equivalent to
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
s = s + std::numeric_limits<size_t>::max();
So it looks like E1 op = E2 is not equivalent to E1 = E1 op E2.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Nicolas Lesser
2018-10-12 23:20:49 UTC
Permalink
We have: s += strlen(s) - 1; (#0)
This is equivalent to (as per the standard to):

s = (s) + (strlen(s) - 1);

Note the parentheses. For (s), they are not important, but they are for the
second expression.

s = s + strlen(s) -1 is not equivalent to s = s + (strlen(s) - 1)
The difference is that the + operator groups from left-to-right, so you
actually have:

s = (s + strlen(s)) - 1 (#1) is not equivalent to s = s + (strlen(s) - 1)
(#2)

Now note that you cannot say that #1 is equivalent to #0, because #1 does
not have the form:

E1 = E1 op E2

It sure looks like it without the parentheses, but that's misleading. If
you imagine an AST for the second operand of the assignment, the standard
requires that it is a operator+ node with the first operand being E1 and
the second one being E2.
For #1, you can see that the first operand of the operator+ node is not E1,
but (E1 + strlen(s)). Thus, there is no problem and the standard is correct.

Hope that helps :)

On Sat, Oct 13, 2018 at 1:10 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
Could you elaborate>
суббПта, 13 Пктября 2018 г., 3:00:16 UTC+4 пПльзПватель Nicolas Lesser
Post by Nicolas Lesser
What you're missing is how the plus operator groups.
On Sat, Oct 13, 2018, 12:51 AM 'Vlad from Moscow' via ISO C++ Standard -
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
In the C++ Standard there is written according to the compound operator
( *8.5.18 Assignment and compound assignment operators, p.#7):*
7 The behavior of an expression of the form E1 *op *= E2 is equivalent
to E1 = E1 *op *E2 except that E1 is
evaluated only once.
However I doubt whether this phrase is correct.
Consider the following code snippet
s = s + strlen( s ) - 1;
where s has type char *. If s is a pointer to an empty string then the
statement is equivalent to
s = s - 1;
However if to rewrite the statement like
s += strlen( s ) - 1;
This is not equivalent to the above, as you changed the grouping order.
s = s + (strlen(s) - 1);
Which suffers from the same problem.
The simple assignment above does not have an equivalent compound
assignment version.
Then the statement is equivalent to
Post by 'Vlad from Moscow' via ISO C++ Standard - Discussion
s = s + std::numeric_limits<size_t>::max();
So it looks like E1 op = E2 is not equivalent to E1 = E1 op E2.
--
---
You received this message because you are subscribed to the Google
Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an
Visit this group at
https://groups.google.com/a/isocpp.org/group/std-discussion/.
--
---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+***@isocpp.org.
To post to this group, send email to std-***@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
Continue reading on narkive:
Loading...