diff options
author | Benjamin Barenblat <bbaren@google.com> | 2019-06-17 10:04:05 -0400 |
---|---|---|
committer | Benjamin Barenblat <bbaren@google.com> | 2019-06-17 10:04:05 -0400 |
commit | ed71db1e8fc024a3c1b13268653f0a8812c56e34 (patch) | |
tree | 021ecff0d3f08162bf202d2c32860cb394bfb7a5 /src/base/utils.h | |
parent | 6eed60043b7b93407982d9fd3f1822aaf46afc17 (diff) |
C++11 restricts the bodies of constexpr functions more aggressively than
C++14 or later. Introduce and use a C++11-compatible macro for
assertions in constexpr functions.
Diffstat (limited to 'src/base/utils.h')
-rw-r--r-- | src/base/utils.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/base/utils.h b/src/base/utils.h index 0a4fabd..610066b 100644 --- a/src/base/utils.h +++ b/src/base/utils.h @@ -32,4 +32,21 @@ #define UTILS_RELEASE_ASSERT(x) assert(x) #endif +// In C++11, `assert` can't be used portably within constexpr functions. +// ASTC_CONSTEXPR_ASSERT functions as a runtime assert but works in C++11 +// constexpr functions. Example: +// +// constexpr double Divide(double a, double b) { +// return ASTC_CONSTEXPR_ASSERT(b != 0), a / b; +// } +// +// This macro is based on ABSL_ASSERT. +#ifdef NDEBUG +#define ASTC_CONSTEXPR_ASSERT(expr) \ + (false ? static_cast<void>(expr) : static_cast<void>(0)) +#else +#define ASTC_CONSTEXPR_ASSERT(expr) \ + ((expr) ? static_cast<void>(0) : [] { assert(false && #expr); }()) +#endif + #endif // ASTC_CODEC_BASE_UTILS_H_ |