Skip to content

Instantly share code, notes, and snippets.

@vietlq
Last active May 12, 2018 16:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vietlq/7e3d1aecb595b6c1ba0d0d09d333d47c to your computer and use it in GitHub Desktop.
Save vietlq/7e3d1aecb595b6c1ba0d0d09d333d47c to your computer and use it in GitHub Desktop.
C++ (GCC / Clang): Warn on unused function result/return for safety
// https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/Function-Attributes.html
// https://infektor.net/posts/2017-01-19-using-cpp17-attributes-today.html
// Compiler flags: -Wall -Werror -Wextra -pedantic -Wunused-result -std=c++11
// Suggested macro names: HANDLE_OUTPUT / NODISCARD / MUST_HANDLE
#define HANDLE_OUTPUT __attribute__((warn_unused_result))
HANDLE_OUTPUT int critical_func()
{
return 1234;
}
template<typename ... Args>
void ignore(Args && ...)
{
}
int use_critical_func()
{
// This will cause the warning: [x86-64 gcc 7.2 #1] error: ignoring return value of 'int critical_func()',
// declared with attribute warn_unused_result [-Werror=unused-result]
//critical_func();
// This will pass
ignore(critical_func());
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment