kwierman
kwierman

Reputation: 461

Casting from std::size_t to void

In a lot of older code, I've seen variations on the following snippet:

std::size_t  some_size = some_function_that_returns_size_t();
(void)some_size;
assert(some_size > some_other_size);

What is the purpose of the cast to void?

Bear in mind this is not casting to void*

If I has to guess, this looks like a safety check to ensure that the machine size_t is larger than a void at compile time.

Playing around in an interpreter reveals that the cast will return a value different than some_size if the initial value is large enough, but since it's not being assigned, I honestly don't see the point.

In case it matters, this was found in an allocation library as part of the allocation checks against alignment and size.

Upvotes: 0

Views: 503

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 311156

As far as I know such a trick is used to prevent the compiler to issue a warning that a variable was defined but was not used.

Upvotes: 6

Related Questions