Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uint32_t destructor return value

Tags:

c++

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?

like image 603
user1507133 Avatar asked Aug 02 '12 21:08

user1507133


2 Answers

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
like image 189
Luchian Grigore Avatar answered Oct 23 '22 21:10

Luchian Grigore


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.
like image 24
Cheers and hth. - Alf Avatar answered Oct 23 '22 22:10

Cheers and hth. - Alf