• 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 // Mark functions as constexpr if C++20 or newer
19 #if __cplusplus >= 202002L
20 #define PW_CONSTEXPR_CPP20 constexpr
21 #else
22 #define PW_CONSTEXPR_CPP20
23 #endif  // __cpp_constexpr >= 201304L
24 
25 // Mark functions as consteval if supported.
26 #if defined(__cpp_consteval) && __cpp_consteval >= 201811L
27 #define PW_CONSTEVAL consteval
28 #else
29 #define PW_CONSTEVAL constexpr
30 #endif  // __cpp_consteval >= 201811L
31 
32 // Mark functions as constinit if supported by the compiler.
33 #if defined(__cpp_constinit) && __cpp_constinit >= 201907L
34 #define PW_CONSTINIT constinit
35 #elif defined(__clang__)
36 #define PW_CONSTINIT [[clang::require_constant_initialization]]
37 #elif defined(__GNUC__) && __GNUC__ >= 13
38 #define PW_CONSTINIT __constinit
39 #else
40 #define PW_CONSTINIT
41 #endif  // __cpp_constinit
42 
43 // nodiscard with a string literal is only available in later versions (~C++20).
44 #if __cplusplus >= 202002L
45 #define PW_NODISCARD_STR(str) [[nodiscard(str)]]
46 #else
47 #define PW_NODISCARD_STR(str) [[nodiscard]]
48 #endif
49