Strangely, neither -Wunused-result nor -Wall worked in g++ v4.6.3. However, the function attribute which Steve suggested did.
int GenerateRandomNumber() __attribute__((warn_unused_result));
int GenerateRandomNumber()
{
return 4;
}
int main()
{
GenerateRandomNumber();
return 0;
}
results in
**** Build of configuration Debug for project unused_return_value ****
make all Building file: ../src/unused_return_value.cpp Invoking: GCC
C++ Compiler g++ -O0 -g3 -pedantic -Wall -c -fmessage-length=0
-Wunused-result -MMD -MP -MF"src/unused_return_value.d" -MT"src/unused_return_value.d" -o "src/unused_return_value.o" "../src/unused_return_value.cpp" ../src/unused_return_value.cpp: In
function ‘int main()’: ../src/unused_return_value.cpp:10:24: warning:
ignoring return value of ‘int GenerateRandomNumber()’, declared with
attribute warn_unused_result
[-Wunused-result] Finished building:
../src/unused_return_value.cpp Building target: unused_return_value
Invoking: GCC C++ Linker g++ -o "unused_return_value"
./src/unused_return_value.o Finished building target:
unused_return_value
**** Build Finished ****
I might wrap the attribute in a #define.
I am also pushing strongly to use the "treat warnings as errors" option. I firmly believe that anyone who can craft a compiler knows more about the language than me (I struggled with the Dragon Book), so can never understand when people shrug ans say "it's only a warning".
I will also be pushing to use a Linter. And for code to be compiler and linter warning free before code review (results of both to be submitted with the code) and before release to version control.
Thanks, Steve!