All this time I assumed that the function really returns something, it MUST be set to some variable, otherwise, where would the value be returned ?!
It goes into bit-bit. wikipedia article: http://en.wikipedia.org/wiki/Bit_bucket
Example:
std::set<std::string> set_of_strings; ... set_of_strings.insert (some_string);
Here std::set::insert() returns a std::pair , an iterator pointing to the element in the set and bool indicating whether the element was added. In this case, the returned std::pair will just disappear. In many cases, you don't care if an element with the same value is present. In such cases, there is no reason to check the second return value. In many cases, you also do not care about the iterator. So just let it go.
Some overly pedantic programmers will introduce the following:
std::set<std::string> set_of_strings; ... (void) set_of_strings.insert (some_string);
(void) supposedly tells the compiler to ignore the return result. However, the compiler does not need to report this. It will ignore the return result if it is not used. What is supposed (void) is to tell code readers that the return value is intentionally ignored.
However, you will never do this:
(void) x = 42.0; (void) a = b = c = 42;
Both of these assignment operators return a value. (You would not be able to say a = b = c = 42 if this is not so.) This is just one of many examples showing that you always send data to a bucket of bits without knowing it.
My recommendation: Do not use (void) to declare that you intentionally ignore the return result. He doesn’t need a compiler, he doesn’t need a reasonable code reader, and it just makes the programmer who wrote it look arrogant know-how, which most likely knows a lot less than they think.
David hammen
source share