I'd probably write something like:
return const_cast<void*>(""); // non-null pointer, referand doesn't matter
Perhaps the callback function has a pointer input guaranteed not to be null. If so then you could return that.
reinterpret_cast<void*>(1)
is supposed to look ugly, because it's not guaranteed to work. Most architectures don't mind you doing that, but
- the standard doesn't actually guarantee that you can use a pointer value that isn't the address of any object (or a null pointer, or one-off-the-end of an array).
- the standard doesn't guarantee that
reinterpret_cast<void*>(1)
is not a null pointer, that's implementation-defined.
I doubt there really is an implementation in which your function returns null by accident, but there's allowed to be. Imagine a hypothetical implementation in which the CPU's own addressing is 4-bit rather than 8-bit. Of course the implementer would still choose CHAR_BIT == 8
, and all pointers in C++ would be 8-bit aligned. Then the implementation could legally and fairly reasonably map from pointers to integers by leaving the bit pattern unchanged, and from integers to pointers by zeroing the last bit. Alternatively it could right-shift and left-shift by 1 bit. The standard requires that pointer -> integer -> pointer restores the original value, but doesn't require that integer -> pointer -> integer does.