3

Is it just the reinterpret_cast?

int *pointer;
uintptr_t value;
value == reinterpret_cast<uintptr_t>(pointer);
embedc
  • 1,485
  • 1
  • 6
  • 20

2 Answers2

4

Depends on your goal really.

[expr.reinterpret.cast]

4 A pointer can be explicitly converted to any integral type large enough to hold it. The mapping function is implementation-defined. [ Note: It is intended to be unsurprising to those who know the addressing structure of the underlying machine.  — end note ] A value of type std​::​nullptr_­t can be converted to an integral type; the conversion has the same meaning and validity as a conversion of (void*)0 to the integral type.

5 A value of integral type or enumeration type can be explicitly converted to a pointer. A pointer converted to an integer of sufficient size (if any such exists on the implementation) and back to the same pointer type will have its original value; mappings between pointers and integers are otherwise implementation-defined.

The mapping is implementation defined (obviously). If you wish to check that the value of pointer was used to initialize value, then your check is insufficient. The above doesn't promise that reinterpret_cast<uintptr_t>(pointer) will always yield the same integer, even though all sane implementations today do.

I would do the check in reverse, since we have a round trip guarantee:

reinterpret_cast<int*>(value) == pointer;

But even then, it's a pretty weak guarantee. I would not faff about with these conversions too much if I were you. It may be worth to reconsider your design.

StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
2

If you follow the standard to the letter, you ought to use

value == (uintptr_t)(void*)pointer

or using reinterpret_cast:

value == reinterpret_cast<uintptr_t>(reinterpret_cast<void*>(pointer))

which personally I find less readable. Naturally the compiler will remove all the "fluff".

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Thanks! Do we really need reinterpret_cast(pointer) and not just static_cast(pointer)? – embedc Mar 14 '19 at 10:36
  • @embedc: I'd plump for `reinterpret_cast` as it's more descriptive of what you're doing. But even better is to use the c-style cast. See also https://stackoverflow.com/questions/310451/should-i-use-static-cast-or-reinterpret-cast-when-casting-a-void-to-whatever, even though the cast is the other way round in that question, but the answers are comprehensive. – Bathsheba Mar 14 '19 at 10:40
  • Well, the C++ standard defers to another standard for this :P https://port70.net/~nsz/c/c11/n1570.html#7.20.1.4 – StoryTeller - Unslander Monica Mar 14 '19 at 10:47