Teuchos Package Browser (Single Doxygen Collection)
Version of the Day
|
#include "TeuchosCore_config.h"
Go to the source code of this file.
Macros | |
#define | TEUCHOS_UNREACHABLE_RETURN_IMPL(dummyReturnVal) return dummyReturnVal |
#define | TEUCHOS_UNREACHABLE_RETURN(dummyReturnVal) TEUCHOS_UNREACHABLE_RETURN_IMPL(dummyReturnVal) |
Avoid warning about unreachable or missing return from function. More... | |
#define TEUCHOS_UNREACHABLE_RETURN_IMPL | ( | dummyReturnVal | ) | return dummyReturnVal |
Definition at line 56 of file Teuchos_CompilerCodeTweakMacros.hpp.
#define TEUCHOS_UNREACHABLE_RETURN | ( | dummyReturnVal | ) | TEUCHOS_UNREACHABLE_RETURN_IMPL(dummyReturnVal) |
Avoid warning about unreachable or missing return from function.
Consider a function like:
{code} int func(const ESomeEnum val) { switch (val) { case VAL1: return 1; case VAL2: return 2; default: TEUCHOS_TEST_FOR_EXCEPT(true); } } {code}
That code will never execute out of the switch statement. However, some compilers will provide a warning that the function may not return a value. Therefore, one can remove this warning by adding a dummy return value like:
{code} int func(const ESomeEnum val) { switch (val) { case VAL1: return 1; case VAL2: return 2; default: TEUCHOS_TEST_FOR_EXCEPT(true); } return -1; // Will never get called! } {code}
That removes the "may not return value" warning on those compilers. But other compilers will correctly warn that return -1;
will never be executed with a warning like "statement is unreachable". Therefore, to address warnings like this, this macro is used like:
{code} int func(const ESomeEnum val) { switch (val) { case VAL1: return 1; case VAL2: return 2; default: TEUCHOS_TEST_FOR_EXCEPT(true); } TEUCHOS_UNREACHABLE_RETURN(-1); } {code}
On compilers that warn about the return being unreachable the return statement is skipped. On every other compiler, the return statement is kept which results in safer code under refactoring (by avoiding undefined behavior when returning from a function by fall-through without returning an explicit value.
Definition at line 131 of file Teuchos_CompilerCodeTweakMacros.hpp.