Topic: What's the rationale for the rule of comma operator?
Author: Lighter <cqulyx@gmail.com>
Date: Thu, 21 Jun 2007 13:58:46 CST Raw View
In 5.3.3.4 of the standard, the standard provides that "The lvalue-to-
rvalue(4.1), array-to-pointer(4.2),and function-to-pointer(4.3)
standard conversions are not applied to the operand of sizeof."
I think this rule is easy to understand. Because I can find the
contexts of applying the rule as follows.
(1)
int* p = 0;
int b1 = sizeof(*p); // OK, b1 = 4, *p would not be evaluated.
(2)
int array[2];
int b2 = sizeof(array); // OK, b2 = 8, array will not be automatically
converted to a pointer to int
(3)
int b3 = sizeof(strlen); // error, strlen will not be automatically
converted to a function pointer.
So far, everything is understandable.
==========================================
However, In 5.18 of the standard, I find a similar rule on comma
operator: "The lvalue-to-rvalue(4.1), array-to-pointer(4.2),and
function-to-pointer(4.3) standard conversions are not applied to the
left expression."
I tried my best to guess the reason of the latter rule, but failed.
Even, I could not find an example to examine the rule.
As far as I know, C99 didn't provide the rule. My confusion lies in
the reason for making such a rule. Could you give me an example
illustrating that some conversions are applied on the left expression
(or the left operand)? I could not find such an example. If there were
no such an example. Why did the standard provide such a rule?
I cannot even see how one would check whether the conversion happens
or not. Since one couldn't check whether the conversion happens or
not, WHY did the C++ standard explicitly provide such a rule which
doesn't exist in the C language standard? What confused me is just the
question.
Any help will be highly appreciated. Thanks in advance.
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: Hyman Rosen <hyrosen@mail.com>
Date: Fri, 22 Jun 2007 04:29:57 CST Raw View
If the left operand of a comma operator had the
lvalue-to-rvalue conversion applied, the following
program would become illegal
extern struct A a;
int main() { return a, 0; }
because the conversion may not be applied to an
object of incomplete type.
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: ark@acm.org ("Andrew Koenig")
Date: Fri, 22 Jun 2007 09:31:22 GMT Raw View
"Lighter" <cqulyx@gmail.com> wrote in message
news:1182447043.821259.305500@x35g2000prf.googlegroups.com...
> As far as I know, C99 didn't provide the rule. My confusion lies in
> the reason for making such a rule. Could you give me an example
> illustrating that some conversions are applied on the left expression
> (or the left operand)? I could not find such an example. If there were
> no such an example. Why did the standard provide such a rule?
Part of the reason for the rule is to ensure that when someone writes code
such as
(std::cout << foo), bar();
the implementation is not permitted to reject it even though std::cout is
not copyable.
---
[ 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.comeaucomputing.com/csc/faq.html ]
Author: James Kanze <james.kanze@gmail.com>
Date: Fri, 22 Jun 2007 19:36:38 CST Raw View
On Jun 22, 11:31 am, a...@acm.org ("Andrew Koenig") wrote:
> "Lighter" <cqu...@gmail.com> wrote in message
> news:1182447043.821259.305500@x35g2000prf.googlegroups.com...
> > As far as I know, C99 didn't provide the rule. My confusion lies in
> > the reason for making such a rule. Could you give me an example
> > illustrating that some conversions are applied on the left expression
> > (or the left operand)? I could not find such an example. If there were
> > no such an example. Why did the standard provide such a rule?
> Part of the reason for the rule is to ensure that when someone writes code
> such as
> (std::cout << foo), bar();
> the implementation is not permitted to reject it even though std::cout is
> not copyable.
What's the relationship between copiable and the rest? Copiable
has nothing to do with array to pointer or function to pointer
conversions, and for a class type, it doesn't involve an
lvalue to rvalue conversion.
Does lvalue to rvalue conversion apply to class types, anyway?
I can't think of a case where it would. You copy a class by
calling the copy constructor, which means binding to a
reference, and not lvalue to rvalue conversion. In fact, all
legal operations on a class type are "lvalue-neutral", I think.
The only place I see where lvalue-ness plays a role is in
determining whether you can bind to a non-const reference or
not.
(But I've probably overlooked something fundamental.)
--
James Kanze (GABI Software, from CAI) email:james.kanze@gmail.com
Conseils en informatique orient e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S mard, 78210 St.-Cyr-l' cole, France, +33 (0)1 30 23 00 34
---
[ 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.comeaucomputing.com/csc/faq.html ]