blob: 2fc7a2a6d35b30d990a2ad37db78b9edbfcd6c19 [file] [log] [blame]
Wyatt Hepler6c331ae2020-08-04 10:05:11 -07001// 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.
25#if __cplusplus >= 201103L
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 Hepler8e59f4d2020-08-27 10:34:21 -070049#undef __cpp_static_assert
50#define __cpp_static_assert 201411L
Wyatt Hepler6c331ae2020-08-04 10:05:11 -070051
52#define static_assert(...) \
53 static_assert(::pw::polyfill::internal::StaticAssertExpression(__VA_ARGS__), \
54 #__VA_ARGS__)
55
56namespace pw {
57namespace polyfill {
58namespace internal {
59
60constexpr bool StaticAssertExpression(bool expression) { return expression; }
61
62constexpr 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
71#endif // __cplusplus >= 201103L