This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: "gcc" complains about a constant being non constant...






Bill McEnaney wrote:
Maybe we need to distinguish between a const variable and a literal.
if, in line 8, sizeof(days) is a constant,  AND in line 9,
sizeof(String) is
a constant, then how can their division (in line 10) NOT be a constant?
Simple: sizeof(days) is a constant, but sizeof_days is not. Reason:
because
the C language standard says so. A const variable is not a constant.
It'd be
---
   Sorry to say, but whoever the 'rocket scientist' that thought up
this nonsense was, should be shot.

I can use sizeof(days)/sizeof(String)-1 as a direct initializer into
a 'const' variable -- and THAT works:
typedef const char * String;

static const String suffixes [] = {"B", "KB", "MB", "GB", "TB"};

static const int index_of_last_suffix = sizeof(suffixes)/(sizeof (String)) - 1;

As long as I don't try to assign "sizeof(suffixes)/sizeof(String) to
a 'const' declared integer, first, and then use that as an intermediate
step, I'm fine. But If I want to first assign the quotient to a const int
for 'num_items', and then use that value with "-1" to init a 2nd constant, it fails.


Sure seems like a C-design bug -- i.e. may be designed that way, but
it's certainly non-intuitive to see
const int a=b/c;
const int d=a-1; FAIL, while
const int d=b/c-1; works

Certainly breaks or voids the concept of equivalent expressions being
equal.

Do I remember incorrectly, or is this fixed in C++?

Maybe it should be 'fixed' in gnu-c unless they are running under strict
POSIX-anal-retentive mode?  ;^)

Sigh...


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]