Teuchos Package Browser (Single Doxygen Collection) Version of the Day
Loading...
Searching...
No Matches
Macros
Teuchos_CompilerCodeTweakMacros.hpp File Reference
#include "TeuchosCore_config.h"
Include dependency graph for Teuchos_CompilerCodeTweakMacros.hpp:
This graph shows which files directly or indirectly include this file:

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.
 

Macro Definition Documentation

◆ TEUCHOS_UNREACHABLE_RETURN_IMPL

#define TEUCHOS_UNREACHABLE_RETURN_IMPL ( dummyReturnVal)     return dummyReturnVal

Definition at line 56 of file Teuchos_CompilerCodeTweakMacros.hpp.

◆ TEUCHOS_UNREACHABLE_RETURN

#define TEUCHOS_UNREACHABLE_RETURN ( dummyReturnVal)     TEUCHOS_UNREACHABLE_RETURN_IMPL(dummyReturnVal)

Avoid warning about unreachable or missing return from function.

Consider a function like:

int func(const ESomeEnum val)
{
switch (val) {
case VAL1: return 1;
case VAL2: return 2;
default: TEUCHOS_TEST_FOR_EXCEPT(true);
}
}
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call.

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:

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!
}

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:

int func(const ESomeEnum val)
{
switch (val) {
case VAL1: return 1;
case VAL2: return 2;
default: TEUCHOS_TEST_FOR_EXCEPT(true);
}
}
#define TEUCHOS_UNREACHABLE_RETURN(dummyReturnVal)
Avoid warning about unreachable or missing return from function.

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.