Wyatt Hepler | 6c331ae | 2020-08-04 10:05:11 -0700 | [diff] [blame] | 1 | // Copyright 2020 The Pigweed Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 4 | // use this file except in compliance with the License. You may obtain a copy of |
| 5 | // the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | // License for the specific language governing permissions and limitations under |
| 13 | // the License. |
| 14 | |
| 15 | // This file provides adapters for newer C++ language features so that they can |
| 16 | // be used in older versions of C++ (though the code will not function exactly |
| 17 | // the same). This file is not on an include path and is intended to be used |
| 18 | // with -include when compiling C++. |
| 19 | // |
| 20 | // pw_polyfill/language_feature_macros.h provides macro wrappers for a few |
| 21 | // specific uses of modern C++ keywords. |
| 22 | #pragma once |
| 23 | |
| 24 | // C++11 is required for the features in this header. |
Wyatt Hepler | 0a6f763 | 2020-10-29 09:08:19 -0700 | [diff] [blame] | 25 | #if defined(__cplusplus) && __cplusplus >= 201103L |
Wyatt Hepler | 6c331ae | 2020-08-04 10:05:11 -0700 | [diff] [blame] | 26 | |
| 27 | // If consteval is not supported, use constexpr. This does not guarantee |
| 28 | // compile-time execution, but works equivalently in constant expressions. |
| 29 | #ifndef __cpp_consteval |
| 30 | #define consteval constexpr |
| 31 | #endif // __cpp_consteval |
| 32 | |
| 33 | // If constinit is not supported, use a compiler attribute or omit it. If |
| 34 | // omitted, the compiler may still constant initialize the variable, but there |
| 35 | // is no guarantee. |
| 36 | #ifndef __cpp_constinit |
| 37 | #ifdef __clang__ |
| 38 | #define constinit [[clang::require_constant_initialization]] |
| 39 | #else |
| 40 | #define constinit |
| 41 | #endif // __clang__ |
| 42 | #endif // __cpp_constinit |
| 43 | |
| 44 | // This is an adapter for supporting static_assert with a single argument in |
| 45 | // C++11 or C++14. Macros don't correctly parse commas in template expressions, |
| 46 | // so the static_assert arguments are passed to an overloaded C++ function. The |
| 47 | // full stringified static_assert arguments are used as the message. |
| 48 | #if __cpp_static_assert < 201411L |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 49 | #undef __cpp_static_assert |
| 50 | #define __cpp_static_assert 201411L |
Wyatt Hepler | 6c331ae | 2020-08-04 10:05:11 -0700 | [diff] [blame] | 51 | |
| 52 | #define static_assert(...) \ |
| 53 | static_assert(::pw::polyfill::internal::StaticAssertExpression(__VA_ARGS__), \ |
| 54 | #__VA_ARGS__) |
| 55 | |
| 56 | namespace pw { |
| 57 | namespace polyfill { |
| 58 | namespace internal { |
| 59 | |
| 60 | constexpr bool StaticAssertExpression(bool expression) { return expression; } |
| 61 | |
| 62 | constexpr bool StaticAssertExpression(bool expression, const char*) { |
| 63 | return expression; |
| 64 | } |
| 65 | |
| 66 | } // namespace internal |
| 67 | } // namespace polyfill |
| 68 | } // namespace pw |
| 69 | |
| 70 | #endif // __cpp_static_assert < 201411L |
Wyatt Hepler | 0a6f763 | 2020-10-29 09:08:19 -0700 | [diff] [blame] | 71 | #endif // defined(__cplusplus) && __cplusplus >= 201103L |