Topic: Incompatibility C++ <-> ANSI-C
Author: krotoff@such.srcc.msu.su (Alexander Krotoff)
Date: 1996/02/01 Raw View
mkt@isun04.inf.uni-jena.de wrote:
> Does the operator ?: an itegral promotion on its
> second and third operand in C++?
> I cannot find the answer in the ARM. (I think it says: no)
>
> Consider this:
> char x, y;
> Which type has the following expression:
> (a ? x : y)
>
> In ANSI-C the type is clearly an 'int' (or 'unsigned').
> But the type in C++ is 'char'???
>
> Even different compilers have different results!
> (Borland 3.1: 'char', SPARCompiler 4.0: 'int')
I think Borland is right.
Working papers (5.16 Conditional operator) says:
2 If the second and the third operands are lvalues and have the same
type (before any implicit conversions), the result is an lvalue of
that type.
Since both second and third operans are lvalues no implicit
conversion will be performed.
Almost the same I found in the ARM (5.16, Russian edition).
--
Alexander N. Krotoff krotoff@such.srcc.msu.su
Research Computer Center tel: +7(095)939-2638
Moscow State University fax: +7(095)939-4430
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/01/30 Raw View
In article h7q@fsuj01.rz.uni-jena.de, mkt@isun04.inf.uni-jena.de (Tilo Koerbs) writes:
>Does the operator ?: an itegral promotion on its
>second and third operand in C++?
>I cannot find the answer in the ARM. (I think it says: no)
>Consider this:
>char x, y;
>Which type has the following expression:
>(a ? x : y)
>
>In ANSI-C the type is clearly an 'int' (or 'unsigned').
>But the type in C++ is 'char'???
Yes, char. If the second and third operands ('x' and 'y' in the example)
have the same type, that is also the result type. Otherwise the usual
conversions are performed to bring them to a common type. In addition,
if the operands are lvalues, the result is also an lvalue.
These are differences from C. The different rule means that you can
get the expected overloaded function in a call like
foo( B ? x : y );
and that references behave properly. As a side effect, you can also write
(B ? x : y) = 12; // assign 12 to x or y
I'm not sure that is an advantage, however. :-)
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Author: ball@eng.sun.com (Mike Ball)
Date: 1996/01/31 Raw View
In article h7q@fsuj01.rz.uni-jena.de, mkt@isun04.inf.uni-jena.de
(Tilo Koerbs) writes:
> Does the operator ?: an itegral promotion on its
> second and third operand in C++?
> I cannot find the answer in the ARM. (I think it says: no)
>
> Consider this:
> char x, y;
> Which type has the following expression:
> (a ? x : y)
>
> In ANSI-C the type is clearly an 'int' (or 'unsigned').
> But the type in C++ is 'char'???
>
> Even different compilers have different results!
> (Borland 3.1: 'char', SPARCompiler 4.0: 'int')
Not in any version of SPARCompiler C++ that I have access to, which is
all of those from 4.0.1 on. The result is clearly "char". I don't
have a 4.0 version to check.
The test was
extern "C" printf(const char*, ...);
int i;
int main()
{
char a = 0;
char b = 0;
printf("%d\n", sizeof(i ? a : b));
return 0;
}
the result was "1"
-Mike-
---
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]