blob: 28f7b347667908eacd70df1103826b57e9c48d98 [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.
Wyatt Hepler0a6f7632020-10-29 09:08:19 -070025#if defined(__cplusplus) && __cplusplus >= 201103L
Wyatt Hepler6c331ae2020-08-04 10:05:11 -070026
Wyatt Hepler6c331ae2020-08-04 10:05:11 -070027// 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 Hepler8e59f4d2020-08-27 10:34:21 -070032#undef __cpp_static_assert
33#define __cpp_static_assert 201411L
Wyatt Hepler6c331ae2020-08-04 10:05:11 -070034
35#define static_assert(...) \
36 static_assert(::pw::polyfill::internal::StaticAssertExpression(__VA_ARGS__), \
37 #__VA_ARGS__)
38
39namespace pw {
40namespace polyfill {
41namespace internal {
42
43constexpr bool StaticAssertExpression(bool expression) { return expression; }
44
45constexpr 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 Hepler0a6f7632020-10-29 09:08:19 -070054#endif // defined(__cplusplus) && __cplusplus >= 201103L