简体   繁体   中英

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.

据我所知,这种技巧是用来防止编译器发出警告,指出已定义但未使用变量。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM