From 8098758585042329301
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,bc2d9b2f726f122d
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 1994-06-27 02:41:17 PST
Newsgroups: comp.std.c++
Path: bga.com!news.sprintlink.net!hookup!swrinde!pipex!uknet!EU.net!uunet!psinntp!ses.com!jamshid
From: jamshid@ses.com (Jamshid Afshar)
Subject: Re: #if sizeof( int ) == 2
Message-ID: <Cs1Jpy.5B7@ses.com>
Sender: usenet@ses.com
Nntp-Posting-Host: sleepy
Organization: SES, Inc., Austin, TX, USA
References: <BWH.94Jun24202306@kato.prl.ufl.edu>
Date: Mon, 27 Jun 1994 05:39:34 GMT
Lines: 66

In article <BWH.94Jun24202306@kato.prl.ufl.edu>,
Brian Hook <bwh@kato.prl.ufl.edu> wrote:
>
>Borland C++ 3.1 compiles the following code 'correctly':
>
>#if sizeof( int ) == 2
>// assume 16-bit architecture

Btw, this also assumes chars are 8-bits.

>#endif
>
>Watcom C/C++ 10.0 does not, erroring on this.
>
>The ARM states (p. 377):
>
>"After any defined operators are evaluated, any remaining preprocessor
>macros appearing in the constant expression will be prelaced as described
>in $16.3.  The resulting expression must be an integral constant expression
>as defined in $5.19, EXCEPT THAT TYPES INT AND UNSIGNED INT ARE TREATED AS
>LONG AND UNSIGNED LONG RESPECTIVELY, AND IT MAY NOT CONTAIN A CAST, A
>SIZEOF OPERATOR, OR AN ENUMERATION CONSTANT."

Right, Borland (and a few other compilers) allows sizeof() in #if as
an extension.  Don't expect it to work now or later (ANSI/ISO isn't
changing things).

>Also, what is the rationale that a sizeof( variable ) can't be used?  This
>is a major issue with cross platform portability, IMO.  I would assume that
>a sizeof( variable ) would be a constant expression, unless the implicit
>promotion of ints to long ints causes problems in the evaluation.

C (and therefore C++) allow the preprocessing stage to be performed by
a rather dumb program completely separate from the compiler.  Why?
Because preprocessing started out that way and continues to be that
way on many systems.  Allowing sizeof() in #if would have required
either making the preprocessor learn types or would have required
compilers to do preprocessing work.

>Is there another approach that can be used to basically effect the same
>results?

Yes, you can probably use the standard header <limits.h>.  In fact, I
think it's clearer and more directly tests what you want to know.

	#include <limits.h>

	#if UINT_MAX < 0xFFFFFFFF
	typedef long AbsoluteDate;   // int is too small
	#else
	typedef int AbsoluteDate;    // int can go up to at least 65535
	#endif

You can get pretty clever with the constants:

	#include <limits.h>

	// I think this math is valid
	#if ULONG_MAX/0x10000000/0x10000000 >= 0xFF
	typedef long Int64;    // machine with at least 64-bit longs
	#else
	class Int64 {/*...*/};
	#endif

Jamshid Afshar
jamshid@ses.com


