Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
cpp14_inline.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_CPP14_INLINE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_CPP14_INLINE_HPP_
7
12// IWYU pragma: private
13// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
14
15// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16// parasoft-end-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
17
18namespace arene {
19namespace base {
20namespace detail {
21
22/// @brief A helper for C++14 inline variable emulation.
23///
24/// @tparam ObjectT The type of the object to create.
25template <typename ObjectT>
26struct cpp14_inline_static_const {
27 /// @brief The instantiated object.
28 static constexpr ObjectT value{};
29};
30
31template <typename ObjectT>
32constexpr ObjectT cpp14_inline_static_const<ObjectT>::value;
33
34} // namespace detail
35} // namespace base
36} // namespace arene
37
38// parasoft-begin-suppress AUTOSAR-A16_0_1-d "This macro handles replacement depending on C++ version"
39// parasoft-begin-suppress AUTOSAR-A16_0_1-a "Conditional defines permitted by A16-0-1 Permit #2"
40// parasoft-begin-suppress AUTOSAR-M16_0_6-a "'type' and 'name' cannot be parenthesized in these contexts"
41// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
42// object used in multiple TUs."
43// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
44// object used in multiple TUs."
45
46// parasoft-begin-suppress AUTOSAR-A2_7_2-a "False positive: no commented-out code, this is documentation"
47/// @def ARENE_CPP14_INLINE_VARIABLE
48/// @brief Emulates C++17's inline variable declarations for function objects.
49///
50/// @param type The type of the function object to instantiate
51/// @param name The name the instantiated object should have in the containing namespace.
52///
53/// This macro is used to correctly declare the instantiation of function objects in a way compatible with both C++14
54/// and >= C++17 contexts, inspired by Eric Niebler's original paper here:
55/// https://ericniebler.com/2014/10/21/customization-point-design-in-c11-and-beyond/
56///
57/// In a <= C++17 context, the type is simply declared as <c> inline constexpr type name{} </c>. In C++14 contexts,
58/// where @c inline variable declarations are not permitted, the same effect is achieved by indirecting through the
59/// @c arene::base::detail::cpp14_inline_static_const helper template. The usage looks like so:
60///
61/// \snippet docs/examples/compiler_support_examples.cpp arene_cpp14_inline_variable_fo
62///
63#if ARENE_IS_ON(ARENE_STD_INLINE_VARIABLES)
64// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) This cannot be a constexpr function, it must be a macro.
65#define ARENE_CPP14_INLINE_VARIABLE(type, name) \
66 ARENE_MAYBE_UNUSED inline constexpr type name {}
67#else
68// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) This cannot be a constexpr function, it must be a macro.
69#define ARENE_CPP14_INLINE_VARIABLE(type, name) \
70 namespace { /* NOLINT(google-build-namespaces) explicitly want per-TU instantiations*/ \
71 /* NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables, bugprone-macro-parentheses) */ \
72 ARENE_MAYBE_UNUSED constexpr auto& name = ::arene::base::detail::cpp14_inline_static_const<type>::value; \
73 } \
74 ARENE_REQUIRE_SEMICOLON
75#endif
76
77/// @def ARENE_CPP14_INLINE_VARIABLE_INIT
78/// @brief Emulates C++17's inline variable declarations for an arbitrary initializer expression.
79///
80/// @param name The name the instantiated object should have in the containing namespace.
81/// @param ... The initializer expression. Variadic so that initializers containing commas (e.g. template arguments
82/// of the constructed type) do not need extra parentheses at the call site.
83///
84/// Companion to @c ARENE_CPP14_INLINE_VARIABLE for cases where the value cannot be produced by default-construction
85/// (e.g. function objects returned from @c arene::base::bind_back ).
86#if ARENE_IS_ON(ARENE_STD_INLINE_VARIABLES)
87// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) This cannot be a constexpr function, it must be a macro.
88#define ARENE_CPP14_INLINE_VARIABLE_INIT(name, ...) ARENE_MAYBE_UNUSED inline constexpr auto name = __VA_ARGS__
89#else
90// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) This cannot be a constexpr function, it must be a macro.
91#define ARENE_CPP14_INLINE_VARIABLE_INIT_DETAIL_NS(name) name##_cpp14_inline_detail
92// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) This cannot be a constexpr function, it must be a macro.
93#define ARENE_CPP14_INLINE_VARIABLE_INIT_DETAIL_VAR(name) name##_v
94// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) This cannot be a constexpr function, it must be a macro.
95#define ARENE_CPP14_INLINE_VARIABLE_INIT(name, ...) \
96 namespace ARENE_CPP14_INLINE_VARIABLE_INIT_DETAIL_NS(name) { /* NOLINT(google-build-namespaces) */ \
97 template <decltype(nullptr) = nullptr> /* NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) */ \
98 extern constexpr auto ARENE_CPP14_INLINE_VARIABLE_INIT_DETAIL_VAR(name) = __VA_ARGS__; \
99 } \
100 ARENE_MAYBE_UNUSED static constexpr auto const& name = \
101 ARENE_CPP14_INLINE_VARIABLE_INIT_DETAIL_NS(name)::ARENE_CPP14_INLINE_VARIABLE_INIT_DETAIL_VAR(name)<>; \
102 ARENE_REQUIRE_SEMICOLON
103#endif
104
105// parasoft-end-suppress AUTOSAR-A16_0_1-d "This macro handles replacement depending on C++ version"
106// parasoft-end-suppress AUTOSAR-A16_0_1-a "Conditional defines permitted by A16-0-1 Permit #2"
107// parasoft-end-suppress AUTOSAR-M16_0_6-a "'type' and 'name' cannot be parenthesized in these contexts"
108// parasoft-end-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
109// object used in multiple TUs."
110// parasoft-end-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
111// object used in multiple TUs."
112
113#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_CPP14_INLINE_HPP_
Definition accumulate.hpp:37
Definition accumulate.hpp:36