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()
, ie ~(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.
It is not a destructor, it is binary not. Here the invalid index is equal to ~uint32_t(0). Which is a 32 bit unsigned integer with all bits set. ie, 0xffffffff.
它是按位NOT ,它可用于找到1的补码(例如~1011 = 0100)或作为尝试找到2s补码的中间步骤(例如[~1011] + 0001 = 0101)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.