Topic: Sequence Points and comma-Operator
Author: James Kanze <kanze@gabi-soft.de>
Date: Wed, 7 Mar 2001 22:41:43 GMT Raw View
"Andrei Alexandrescu" <andrewalex@hotmail.com> writes:
|> "Ken Hagan" <K.Hagan@thermoteknix.co.uk> wrote in message
|> news:newscache$0f5d9g$zc4$1@firewall.thermoteknix.co.uk...
|> > Overloads of comma, && and !! are (obviously) things that a compiler
|> > can identify and flag. The use of a conventional function is a simple
|> > and suitable replacement in most cases. Almost everyone agrees they
|> > are brain damage. Surely we can deprecate them in a few years time.
|> Expression templates do overload && and || and I don't find that
|> that bad.
But I suspect that they only do so in constant expressions, and
generally, I suspect that you could use & and | just as easily. The
advantage of && and || is the short circuiting, and with overloaded
operators, you no longer have it.
--
James Kanze mailto:kanze@gabi-soft.de
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
Ziegelh ttenweg 17a, 60598 Frankfurt, Germany Tel. +49(069)63198627
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: Thu, 22 Feb 2001 22:24:50 GMT Raw View
John Potter <jpotter@falcon.lhup.edu> wrote:
> On Tue, 20 Feb 2001 22:02:36 GMT, MikeAlpha@NoSpam_csi.com (Martin
> Aupperle) wrote:
>=20
> > is it safe to write=20
> >=20
> > i =3D j, i++;
> >=20
> > i.e. does the standard guarantee that the comma-operator is evaluated
> > from left to right?
>=20
> That's been answered.
Hopefully with a clear 'Yes !'
> > What is in the case when we have a user defined operator?=20
>=20
> i =3D operator,(j, i++);
>=20
> The function call introduces sequence points between the i++ and i=3D.
The precedence - er, grammar - is the other way around:
operator , ( i=3Dj, i++ );
Here we have neither a sequence point, nor a guaranteed order of
evaluation between two arguments. If the assignment and increment
operators used here were builtin ones, the behavior would be undefined.
AFAICS they can't both be builtins though - there must be a user-defined
type involved when overloading the comma operator. But the evaluation
order is still unspecified.
Regards, J=F6rg
--=20
J=F6rg Barfurth joerg.barfurth@attglobal.net
<<<<<<<<<<<<< using std::disclaimer; <<<<<<<<<<<<<<<<<<<<<<<<<<<<
Software Developer http://www.OpenOffice.org
StarOffice Configuration http://www.sun.com/staroffice
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 22 Feb 2001 23:32:28 GMT Raw View
In article <MPG.14fdc11d28eb057598bef2@news.mindspring.com>, Stan Brown
<brahms@mindspring.com> writes
>I think you mean to say that "i=j" is fully evaluated and side
>effects completed before i++ is evaluated.
>
>My understanding is that the above statement is the same as
> (i=j), i++;
I wonder why I forgot that ',' is the lowest precedence operator :(
--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Thu, 22 Feb 2001 23:34:36 GMT Raw View
In article <3A93CCC4.420AC133@acm.org>, Nathan Sidwell <nathan@acm.org>
writes
> has three arguments, the second of which has the value 5.]
>so the example given is ok, and is
> (i = j), i++
>
>however, if an overloaded operator, is selected, then the expression
>becomes
> operator, (i=j, i++)
>and the sequence point your thought was there has now gone, to be
>replaced
>with some other sequence points. Of course, it's possible (quite
>probable),
>that `i=j' and `i++' also select user defined operators, and hence
>function
>calls. These are guaranteed to occur sequentially, but it is not defined
>as
>to which order they happen in.
That is really evil.
--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Tom Payne <thp@hill.cs.ucr.edu>
Date: Fri, 23 Feb 2001 13:19:03 CST Raw View
Tom Payne <thp@hill.cs.ucr.edu> wrote:
[...]
: IIRC, the comma operator:
: - has the lowest of all priorities,
: - is evaluated, and
^^^^^^^^^^^^
Oops! That should read, "is evaluated left to right".
: - has a sequence point between its operands.
: Please correct me if I'm misinformed.
Tom Payne
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Nathan Sidwell <nathan@acm.org>
Date: Fri, 23 Feb 2001 13:52:02 CST Raw View
Francis Glassborow wrote:
>
> In article <3A93CCC4.420AC133@acm.org>, Nathan Sidwell <nathan@acm.org>
> >calls. These are guaranteed to occur sequentially, but it is not defined
> >as
> >to which order they happen in.
>
> That is really evil.
Yup :-(
comma, && and || are part of the control structure of the language,
and as such, should never have been candidates for overloading.
Too late now though ...
nathan
--
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
Never hand someone a gun unless you are sure where they will point it
nathan@acm.org http://www.cs.bris.ac.uk/~nathan/ nathan@cs.bris.ac.uk
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Valentin.Bonnard@free.fr (Valentin Bonnard)
Date: Mon, 26 Feb 2001 22:57:01 GMT Raw View
John Potter wrote:
> On Tue, 20 Feb 2001 22:02:36 GMT, MikeAlpha@NoSpam_csi.com (Martin
> Aupperle) wrote:
>
>> is it safe to write
>>
>> i = j, i++;
>>
>> i.e. does the standard guarantee that the comma-operator is evaluated
>> from left to right?
>
> That's been answered.
Wrongly. It parses as
(i = j), i++;
and it's perfectly correct.
>> What is in the case when we have a user defined operator?
>
> i = operator,(j, i++);
>
> The function call introduces sequence points between the i++ and i=.
The parsing doesn't change (of couse!) with a user
defined operator:
operator,(i = j, i++);
and there is no sequence point between i = j and i++,
so it's incorrect.
--
Valentin Bonnard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Ken Hagan" <K.Hagan@thermoteknix.co.uk>
Date: Mon, 26 Feb 2001 23:10:51 GMT Raw View
"Nathan Sidwell" <nathan@acm.org> wrote...
>
> comma, && and || are part of the control structure of the language,
> and as such, should never have been candidates for overloading.
>
> Too late now though ...
Is it?
Overloads of comma, && and !! are (obviously) things that a compiler
can identify and flag. The use of a conventional function is a simple
and suitable replacement in most cases. Almost everyone agrees they
are brain damage. Surely we can deprecate them in a few years time.
Or are you suggesting that deprecated features will never actually be
removed from the language?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Andrei Alexandrescu" <andrewalex@hotmail.com>
Date: Sun, 4 Mar 2001 18:20:36 GMT Raw View
"Ken Hagan" <K.Hagan@thermoteknix.co.uk> wrote in message
news:newscache$0f5d9g$zc4$1@firewall.thermoteknix.co.uk...
> Overloads of comma, && and !! are (obviously) things that a compiler
> can identify and flag. The use of a conventional function is a simple
> and suitable replacement in most cases. Almost everyone agrees they
> are brain damage. Surely we can deprecate them in a few years time.
Expression templates do overload && and || and I don't find that that bad.
Andrei
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: MikeAlpha@NoSpam_csi.com (Martin Aupperle)
Date: Tue, 20 Feb 2001 22:02:36 GMT Raw View
Hello,
is it safe to write
i = j, i++;
i.e. does the standard guarantee that the comma-operator is evaluated
from left to right? What is in the case when we have a user defined
operator?
Thanks - Martin
------------------------------------------------
Martin Aupperle
MikeAlpha@NoSpam_csi.com
(remove NoSpam_)
------------------------------------------------
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 21 Feb 2001 00:25:38 GMT Raw View
In article <3a918a1d.208003893@news.nikoma.de>, Martin Aupperle
<MikeAlpha@NoSpam_csi.com> writes
>Hello,
>
>is it safe to write
>
> i = j, i++;
>
>i.e. does the standard guarantee that the comma-operator is evaluated
>from left to right? What is in the case when we have a user defined
>operator?
The only guarantee you have for the built in operator is that j is fully
evaluated and all side effects completed before i++ is evaluated.
However as that is necessarily evaluted before the assignment is
completed you have no guarantees as to when the side effects of
evaluating the increment and the assignment occur, so you have undefined
behaviour.
--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Ron Natalie <ron@spamcop.net>
Date: Wed, 21 Feb 2001 02:07:08 GMT Raw View
Martin Aupperle wrote:
>
> Hello,
>
> is it safe to write
>
> i = j, i++;
>
> i.e. does the standard guarantee that the comma-operator is evaluated
> from left to right? What is in the case when we have a user defined
> operator?
>
The LHS of a comma is evaluated, discarded (a sequence point occurs here)
and then the right hand side. Guaranteed.
However the expression you have is really:
i = (j, i++)
so you still have i changed once between sequence points. Both the
post-increment and the assignment to i occur after the comma-induced
sequence point and before the next one (at the end of the full expression).
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Ken Hagan" <K.Hagan@thermoteknix.co.uk>
Date: Wed, 21 Feb 2001 08:04:11 CST Raw View
"Ron Natalie" <ron@spamcop.net> wrote...
>
>
> Martin Aupperle wrote:
> >
> > is it safe to write
> >
> > i = j, i++;
> >
> However the expression you have is really:
>
> i = (j, i++)
Is it? The precedence tables in K&R2 and CP3L-SE both have ","
quite firmly below "=". The latter does add that some rules can
only be established from reading the grammar, but I hope this
isn't one of them.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: jpotter@falcon.lhup.edu (John Potter)
Date: Wed, 21 Feb 2001 14:05:28 GMT Raw View
On Tue, 20 Feb 2001 22:02:36 GMT, MikeAlpha@NoSpam_csi.com (Martin
Aupperle) wrote:
> is it safe to write
>
> i = j, i++;
>
> i.e. does the standard guarantee that the comma-operator is evaluated
> from left to right?
That's been answered.
> What is in the case when we have a user defined operator?
i = operator,(j, i++);
The function call introduces sequence points between the i++ and i=.
John
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Wed, 21 Feb 2001 17:26:27 GMT Raw View
Martin Aupperle wrote:
>
> Hello,
>
> is it safe to write
>
> i = j, i++;
>
> i.e. does the standard guarantee that the comma-operator is evaluated
> from left to right? ...
Yes, but that's not enough to render that expression useful, for two
related reasons.
There's a common mistake of assuming that there is a sequence point
seperating the left-hand side of an assignment expression from the
right-hand side. No one explicitly says so, because if they bother to
look they'll find it's not there. However, most of the mistakes I've
seen in interpreting the sequence point rules can be explained by that
mistaken assumption. Another, related mistake, is thinking that
assignment is the main point of an assignment expression; the fact that
it also returns a value is secondary. As far as the standard is
concerned, the assignment is "merely" a side-effect, the returned value
is the main point. As a side-effect, the assignment itself is
susceptible to considerable rearrangement by an optimizing compiler.
In this particular case, the absence of a sequence point means that,
first of all, there's no guarantee whether 'i' is updated first by the
increment expression, or by the assignment expression. If 'i' started
out as 2, it could end up as either 2 or 3, depending upon which
side-effect occurs first.
Secondly, and more importantly, modifying the same value twice with no
intervening sequence point allows undefined behavior. NOT just an
undefined value - undefined behavior! Anything is allowed to happen,
according to the standard. Your program could issue your machine's "Halt
and Catch Fire" instruction.
> ... What is in the case when we have a user defined
> operator?
Now, all of the above changes if there's an applicable operator=()
overload. Then this statement becomes equivalent to:
i.operator=(j,i.operator++()));
Operator overloads are function calls, with all of the associated
sequence points. There's a sequence point separating the arguments of a
function call from entrance into the function body, so this suddenly
becomes a legal expression, with fairly predictable results. The
operator++() and operator=() functions will be called, in that order,
with all of operator++()'s side-effects, whatever they are, complete
before the body of operator=() is entered. If a relevant operator,()
applies, it will be called after the operator++() and it's side-effects;
operator,()'s side-effects will be complete before operator=() is
called.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: brahms@mindspring.com (Stan Brown)
Date: Wed, 21 Feb 2001 17:26:48 GMT Raw View
Quoth Francis Glassborow <francis.glassborow@ntlworld.com> in
comp.std.c++:
>In article <3a918a1d.208003893@news.nikoma.de>, Martin Aupperle
><MikeAlpha@NoSpam_csi.com> writes
>>is it safe to write
>> i = j, i++;
>>i.e. does the standard guarantee that the comma-operator is evaluated
>>from left to right? What is in the case when we have a user defined
>>operator?
>
>The only guarantee you have for the built in operator is that j is fully
>evaluated and all side effects completed before i++ is evaluated.
I think you mean to say that "i=j" is fully evaluated and side
effects completed before i++ is evaluated.
My understanding is that the above statement is the same as
(i=j), i++;
Therefore the evaluation is that j's value is assigned to i, and the
i is incremented. The comma has lower precedence than assignment,
no? That' what Stroustrup TC++PL3 says on page 121. On page 123 he
lists the comma operator, along with && and ||, as guaranteeing that
their left operands are evaluated before their right operands. (Of
course with && and || the right operands might never be evaluated,
depending on the left operands.)
In for loops we do this sort of thing all the time:
for (i=something, j=something_else; ...
So I fear I must disagree with both Martin and Francis. I may be
horribly wrong here, but it sure looks to me like Martin's statement
is unambiguous and has no whiff of undefined behavior.
--
Stan Brown, Oak Road Systems, Cortland County, New York, USA
http://oakroadsystems.com
C++ FAQ Lite: http://www.parashift.com/c++-faq-lite/
the C++ standard: http://webstore.ansi.org/
reserved C++ identifiers: http://oakroadsystems.com/tech/cppredef.htm
more FAQs: http://oakroadsystems.com/tech/faqget.htm
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Tom Payne <thp@hill.cs.ucr.edu>
Date: Wed, 21 Feb 2001 13:34:45 CST Raw View
James Kuyper Jr. <kuyper@wizard.net> wrote:
[...]
: Martin Aupperle wrote:
[...]
:> is it safe to write
:>
:> i = j, i++;
:>
:> i.e. does the standard guarantee that the comma-operator is evaluated
:> from left to right? ...
[...]
: In this particular case, the absence of a sequence point means that,
: first of all, there's no guarantee whether 'i' is updated first by the
: increment expression, or by the assignment expression. If 'i' started
: out as 2, it could end up as either 2 or 3, depending upon which
: side-effect occurs first.
: Secondly, and more importantly, modifying the same value twice with no
: intervening sequence point allows undefined behavior. NOT just an
: undefined value - undefined behavior!
IIRC, the comma operator:
- has the lowest of all priorities,
- is evaluated, and
- has a sequence point between its operands.
Please correct me if I'm misinformed.
Tom Payne
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: Nathan Sidwell <nathan@acm.org>
Date: Wed, 21 Feb 2001 19:51:32 GMT Raw View
Ron Natalie wrote:
>
> However the expression you have is really:
>
> i = (j, i++)
>
Um, no.
[5.17] assignment operators
assignment-expression:
conditional-expression
logical-or-expression assignment-operator assignment-expression
throw-expression
[5.18] comma operator
expression:
assignment-expresion
expression , assignment-expression.
... [Example:
f (a (t = 3, t + 2), c);
has three arguments, the second of which has the value 5.]
so the example given is ok, and is
(i = j), i++
however, if an overloaded operator, is selected, then the expression
becomes
operator, (i=j, i++)
and the sequence point your thought was there has now gone, to be
replaced
with some other sequence points. Of course, it's possible (quite
probable),
that `i=j' and `i++' also select user defined operators, and hence
function
calls. These are guaranteed to occur sequentially, but it is not defined
as
to which order they happen in.
nathan
--
Dr Nathan Sidwell :: Computer Science Department :: Bristol University
Never hand someone a gun unless you are sure where they will point it
nathan@acm.org http://www.cs.bris.ac.uk/~nathan/ nathan@cs.bris.ac.uk
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Anthony Williams" <anthwil@nortelnetworks.com>
Date: Thu, 22 Feb 2001 00:32:05 GMT Raw View
"John Potter" <jpotter@falcon.lhup.edu> wrote in message
news:3a9395a1.5405648@news.csrlink.net...
> On Tue, 20 Feb 2001 22:02:36 GMT, MikeAlpha@NoSpam_csi.com (Martin
> Aupperle) wrote:
>
> > is it safe to write
> >
> > i = j, i++;
> >
> > i.e. does the standard guarantee that the comma-operator is evaluated
> > from left to right?
Sequence points are the problem here. This decomposes into
(i=j), (i++)
like the example from 5.18p2:
"[Example:
f(a, (t=3, t+2), c);
has three arguments, the second of which has the value 5. ]"
However, unlike the example, i is modified twice in the same expression. For
builtin types, there are no sequence points within an expression, so the
result is undefined behaviour.
For UDT, it depends on whether or not there are any user-defined operators:
> > What is in the case when we have a user defined operator?
Defining either of "operator=", or "operator++" solves your problem, as
there are sequence points before and after each function call. The grouping
remains unchanged, but i is now only modified once between sequence points
(i.operator=(j)),(i++)
In this case, there is a sequence point, then the result of the assignment
is completed, followed by a sequence point, then i++ is evaluated, then the
comma expression evaluated.
(i=j),(i.operator++())
In this case, the assignment is done, then there is a sequence point, then
the increment operator is called then there is a sequence point, then the
comma expression is evaluated.
Note that a user-defined comma operator doesn't help, as this doesn't
introduce a sequence point between the assignment and the increment.
operator,(i=j,i++)
(i=j) and (i++) are evaluated (in implementation defined order), then there
is a sequence point, then operator,() is called, then there is a sequence
point. Note that this is still undefined if neither "operator=" or
"operator," is defined. Note also, that if they ARE defined, the result is
IMPLEMENTATION defined, since the arguments to function calls may be
evaluated in any order.
Question:
I just described operator,(a,b), to be the same as any other free function -
a and b are evaluated in an implementation defined order, before the
function is invoked.
Does the same apply to a.operator,(b) ?
i.e., is
(i=j).operator,(i++)
still implementation defined (assuming user-defined operator= or
operator++), or is (i=j) guaranteed to be evaluated before (i++)?
Anthony
--
Anthony Williams
Software Engineer, Nortel Networks Optoelectronics
The opinions expressed in this message are not necessarily those of my
employer
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Thu, 22 Feb 2001 19:35:33 GMT Raw View
Tom Payne wrote:
>
> James Kuyper Jr. <kuyper@wizard.net> wrote:
>
> [...]
> : Martin Aupperle wrote:
>
> [...]
> :> is it safe to write
> :>
> :> i = j, i++;
> :>
> :> i.e. does the standard guarantee that the comma-operator is evaluated
> :> from left to right? ...
>
> [...]
> : In this particular case, the absence of a sequence point means that,
> : first of all, there's no guarantee whether 'i' is updated first by the
> : increment expression, or by the assignment expression. If 'i' started
> : out as 2, it could end up as either 2 or 3, depending upon which
> : side-effect occurs first.
>
> : Secondly, and more importantly, modifying the same value twice with no
> : intervening sequence point allows undefined behavior. NOT just an
> : undefined value - undefined behavior!
>
> IIRC, the comma operator:
> - has the lowest of all priorities,
> - is evaluated, and
> - has a sequence point between its operands.
> Please correct me if I'm misinformed.
You're correct. I don't use the comma operator very often, and I forgot
that it had lower precedence than assignment. Therefore, this is an
expression with well-defined behavior.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]