1 // Copyright 2020 The Pigweed Authors
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
7 // https://www.apache.org/licenses/LICENSE-2.0
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
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++.
20 // pw_polyfill/language_feature_macros.h provides macro wrappers for a few
21 // specific uses of modern C++ keywords.
24 // C++11 is required for the features in this header.
25 #if defined(__cplusplus) && __cplusplus >= 201103L
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
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
36 #ifndef __cpp_constinit
38 #define constinit [[clang::require_constant_initialization]]
42 #endif // __cpp_constinit
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
49 #undef __cpp_static_assert
50 #define __cpp_static_assert 201411L
52 #define static_assert(...) \
53 static_assert(::pw::polyfill::internal::StaticAssertExpression(__VA_ARGS__), \
60 constexpr bool StaticAssertExpression(bool expression) { return expression; }
62 constexpr bool StaticAssertExpression(bool expression, const char*) {
66 } // namespace internal
67 } // namespace polyfill
70 #endif // __cpp_static_assert < 201411L
71 #endif // defined(__cplusplus) && __cplusplus >= 201103L