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 | |
Wyatt Hepler | 6c331ae | 2020-08-04 10:05:11 -0700 | [diff] [blame] | 27 | // This is an adapter for supporting static_assert with a single argument in |
| 28 | // C++11 or C++14. Macros don't correctly parse commas in template expressions, |
| 29 | // so the static_assert arguments are passed to an overloaded C++ function. The |
| 30 | // full stringified static_assert arguments are used as the message. |
| 31 | #if __cpp_static_assert < 201411L |
Wyatt Hepler | 8e59f4d | 2020-08-27 10:34:21 -0700 | [diff] [blame] | 32 | #undef __cpp_static_assert |
| 33 | #define __cpp_static_assert 201411L |
Wyatt Hepler | 6c331ae | 2020-08-04 10:05:11 -0700 | [diff] [blame] | 34 | |
| 35 | #define static_assert(...) \ |
| 36 | static_assert(::pw::polyfill::internal::StaticAssertExpression(__VA_ARGS__), \ |
| 37 | #__VA_ARGS__) |
| 38 | |
| 39 | namespace pw { |
| 40 | namespace polyfill { |
| 41 | namespace internal { |
| 42 | |
| 43 | constexpr bool StaticAssertExpression(bool expression) { return expression; } |
| 44 | |
| 45 | constexpr bool StaticAssertExpression(bool expression, const char*) { |
| 46 | return expression; |
| 47 | } |
| 48 | |
| 49 | } // namespace internal |
| 50 | } // namespace polyfill |
| 51 | } // namespace pw |
| 52 | |
| 53 | #endif // __cpp_static_assert < 201411L |
Wyatt Hepler | 0a6f763 | 2020-10-29 09:08:19 -0700 | [diff] [blame] | 54 | #endif // defined(__cplusplus) && __cplusplus >= 201103L |