Today I saw this code, inside a class:
static const uint32_t invalid_index = ~uint32_t();
My question is, what is the return value of a uint32_t
destructor, and why is it useful?
That's not a destructor, but a bitwise NOT
operator applied to a value-initialized uint32_t
.
A value initialized integral type is 0
, so you're taking the bitwise NOT
of 0
.
Similar to:
uint32_t x = uint32_t(); // 32 0's in binary form
uint32_t y = ~x; // 32 1's in binary form
First of all, as many have already mentioned, the code you saw,
static const uint32_t invalid_index = ~uint32_t();
is not a destructor call but the bitwise "not" ~
, applied to the default value of the type, uint32_t()
, i.e. ~(uint32_t(0))
.
Now to your question,
My question is, what is the return value of a uint32_t destructor, and why is it useful?
The return type of the pseudo-destructor (it’s not a real destructor, just a do-nothing operation with the same notation as a destructor call) is void
, and it’s mainly useful for generic programming where you don’t know the type.
Example:
uint32_t x;
x.~uint32_t(); // Silly but valid, a pseudo-destructor call.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With