🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

4 total messages Started by Tilo Koerbs Tue, 30 Jan 1996 15:45
Incompatibility C++ <-> ANSI-C
#3162
Author: Tilo Koerbs
Date: Tue, 30 Jan 1996 15:45
20 lines
1216 bytes
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')



[ 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. ]
Re: Incompatibility C++ <-> ANSI-C
#3143
Author: Steve Clamage
Date: Tue, 30 Jan 1996 17:52
30 lines
1961 bytes
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. ]
Re: Incompatibility C++ <-> ANSI-C
#3166
Author: Mike Ball
Date: Wed, 31 Jan 1996 22:10
40 lines
1689 bytes
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. ]
Incompatibility C++ <-> ANSI-C
#3172
Author: Alexander Krotoff
Date: Thu, 01 Feb 1996 01:51
37 lines
1862 bytes
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. ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads