2

In some legacy code, which compiles fine on GCC 4.6 (with -fpermissive), I have this:

uint16_t a = 0;
void* b = ...;

if(b == a) // ...

Is this comparison well-defined on GCC 4.6? Does it downcast to 16 bits or upcast to 32/64 bits?

Albert
  • 65,406
  • 61
  • 242
  • 386

3 Answers3

1

Although this isn't explicitly written in the C++11 standard (N3337 draft), I was able to come up with this (emphasis mine).

§5.9 Relational operators

Pointers to objects or functions of the same type (after pointer conversions) can be compared, with a result defined as follows

— If two pointers ...

— If two pointers ...

— If two pointers ...

— If two pointers ...

— If two pointers ...

Other pointer comparisons are unspecified.

Now for the equality part:

§5.10 Equality operators

The == (equal to) and the != (not equal to) operators have the same semantic restrictions, conversions, and result type as the relational operators except for their lower precedence and truth-value result.

By this I believe that such a comparison is unspecified.

Community
  • 1
  • 1
legends2k
  • 31,634
  • 25
  • 118
  • 222
  • So you say that it is undefined in the C++ standard. But how would GCC 4.6 do it? – Albert Apr 04 '14 at 13:23
  • I believe that it is _unspecified_ by the standard, not _undefined_. However, in both cases an implemenation (like GCC) is free to do what it may; please see [here](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). – legends2k Apr 04 '14 at 13:23
  • I know. But my question is, what does it do. – Albert Apr 04 '14 at 13:43
1

Looks like it up-casts the 16 bit integer to match the pointer size. Running the following code outputs "upcast"

uint16_t a = 1;
void* b = (void*)0x10001;
(b == a) ? printf("downcast") : printf("upcast");
NickC
  • 385
  • 1
  • 10
0

It may compile or may not (not sure, it depends on the compiler and compiler options) In any case the cast will be performed like:

if( b == (void*)a )
{
}

Note that upcasting/downcasting is not the correct word to use because it is related to classes, in this case is just type conversions.