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
8// IWYU pragma: private
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12#include "arene/base/compiler_support/attributes.hpp"
13#include "arene/base/compiler_support/platform_queries.hpp"
14#include "arene/base/compiler_support/preprocessor.hpp"
15// parasoft-end-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
16
17namespace arene {
18namespace base {
19namespace detail {
20
21/// @brief A helper for C++14 inline variable emulation.
22///
23/// @tparam ObjectT The type of the object to create.
24template <typename ObjectT>
25struct cpp14_inline_static_const {
26 /// @brief The instantiated object.
27 static constexpr ObjectT value{};
28};
29
30template <typename ObjectT>
31constexpr ObjectT cpp14_inline_static_const<ObjectT>::value;
32
33} // namespace detail
34} // namespace base
35} // namespace arene
36
37// parasoft-begin-suppress AUTOSAR-A16_0_1-d "This macro handles replacement depending on C++ version"
38// parasoft-begin-suppress AUTOSAR-A16_0_1-a "Conditional defines permitted by A16-0-1 Permit #2"
39// parasoft-begin-suppress AUTOSAR-M16_0_6-a "'type' and 'name' cannot be parenthesized in these contexts"
40// parasoft-begin-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
41// object used in multiple TUs."
42// parasoft-begin-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
43// object used in multiple TUs."
44
45// parasoft-begin-suppress AUTOSAR-A2_7_2-a "False positive: no commented-out code, this is documentation"
46/// @def ARENE_CPP14_INLINE_VARIABLE
47/// @brief Emulates C++17's inline variable declarations for function objects.
48///
49/// @param type The type of the function object to instantiate
50/// @param name The name the instantiated object should have in the containing namespace.
51///
52/// This macro is used to correctly declare the instantiation of function objects in a way compatible with both C++14
53/// and >= C++17 contexts, inspired by Eric Niebler's original paper here:
54/// https://ericniebler.com/2014/10/21/customization-point-design-in-c11-and-beyond/
55///
56/// In a <= C++17 context, the type is simply declared as <c> inline constexpr type name{} </c>. In C++14 contexts,
57/// where @c inline variable declarations are not permitted, the same effect is achieved by indirecting through the
58/// @c arene::base::detail::cpp14_inline_static_const helper template. The usage looks like so:
59///
60/// \snippet docs/examples/compiler_support_examples.cpp cpp14_inline_variable_fo
61///
62#if ARENE_IS_ON(ARENE_STD_INLINE_VARIABLES)
63// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) This cannot be a constexpr function, it must be a macro.
64#define ARENE_CPP14_INLINE_VARIABLE(type, name)
65 ARENE_MAYBE_UNUSED inline constexpr type name {}
66#else
67// NOLINTNEXTLINE(cppcoreguidelines-macro-usage) This cannot be a constexpr function, it must be a macro.
68#define ARENE_CPP14_INLINE_VARIABLE(type, name)
69 namespace { /* NOLINT(google-build-namespaces) explicitly want per-TU instantiations*/
70 /* NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables, bugprone-macro-parentheses) */
71 ARENE_MAYBE_UNUSED constexpr auto& name = ::arene::base::detail::cpp14_inline_static_const<type>::value;
72 }
73 ARENE_REQUIRE_SEMICOLON
74#endif
75
76// parasoft-end-suppress AUTOSAR-A16_0_1-d "This macro handles replacement depending on C++ version"
77// parasoft-end-suppress AUTOSAR-A16_0_1-a "Conditional defines permitted by A16-0-1 Permit #2"
78// parasoft-end-suppress AUTOSAR-M16_0_6-a "'type' and 'name' cannot be parenthesized in these contexts"
79// parasoft-end-suppress AUTOSAR-M7_3_3-a "An unnamed namespace is used to create a per-TU reference to a global
80// object used in multiple TUs."
81// parasoft-end-suppress CERT_CPP-DCL59-a "An unnamed namespace is used to create a per-TU reference to a global
82// object used in multiple TUs."
83
84#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_COMPILER_SUPPORT_CPP14_INLINE_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10