(void)
casts in C aren’t totally useless.
Sometimes if I don’t care about a C function’s return value, I explicitly discard it with a (void)
cast:
(void) foo();
Why bother? After all, isn’t the return value implicitly discarded simply by not assigning it to anything?
A (void)
cast is a comment to the reader stating, “Yes, the author is aware that this function returns something, has considered the consequences of ignoring possible error values, and just doesn’t care.”
Without the cast, the code says, “Did the author intentionally ignore the return value of this function call? Is there some failure case the author forgot to handle? Can something go wrong here?”
Furthermore, if something does go wrong, the (void)
cast marks a possible failure point.
No Comments Yet »
RSS feed for comments on this post.