• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // Macros for using C++ features in older standards.
16 #pragma once
17 
18 #ifdef __cpp_inline_variables
19 #define PW_INLINE_VARIABLE inline
20 #else
21 #define PW_INLINE_VARIABLE
22 #endif  // __cpp_inline_variables
23 
24 // Mark functions as constexpr if C++20 or newer
25 #if __cplusplus >= 202002L
26 #define PW_CONSTEXPR_CPP20 constexpr
27 #else
28 #define PW_CONSTEXPR_CPP20
29 #endif  // __cpp_constexpr >= 201304L
30 
31 // Mark functions as consteval if supported.
32 #if defined(__cpp_consteval) && __cpp_consteval >= 201811L
33 #define PW_CONSTEVAL consteval
34 #else
35 #define PW_CONSTEVAL constexpr
36 #endif  // __cpp_consteval >= 201811L
37 
38 // Mark functions as constinit if supported by the compiler.
39 #if defined(__cpp_constinit)
40 #define PW_CONSTINIT constinit
41 #elif defined(__clang__)
42 #define PW_CONSTINIT [[clang::require_constant_initialization]]
43 #elif defined(__GNUC__) && __GNUC__ >= 10
44 #define PW_CONSTINIT __constinit
45 #else
46 #define PW_CONSTINIT
47 #endif  // __cpp_constinit
48