You can discard the return value. It's no good style in general, but you can always do it.
That's one of the reasons why we can't overload on the return type. So if you'd define another function void test()
, the call wouldn't be possible because of ambiguity.
If you want your compiler to warn such cases in which you (un)intentionally discard the return value, pass the flag -Wunused-result
(for GCC). [I don't know the flags for other compilers]
In general, almost all return values make sense, so some people use this macro to detect cases where they unintentionally discarded the value, especially if the return value is something like an error code which should be checked afterwards.
If you want to enable the warning for a particular function, you can put an attribute at the end of the signature (also for GCC, for MSVC have a look at this question):
std::vector<int> __attribute__((warn_unused_result)) test() {
return std::vector<int>(1,0);
}
See also: How to raise warning if return value is disregarded?
Thus, if you call the function but don't use the return value, you get a warning. Note that warnings don't make compilation fail, for this you need to add -Werror
to the compiler flags.