Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
Facilities For Creating Inline Variables in C++14

C++17 added support for declaring inline variables, which allow variables to be defined in header files without violating the One Definition Rule (ODR). To allow similar functionality in C++14, arene-base provides a facility, ARENE_CPP14_INLINE_VARIABLE(type,name), for creating "inline variables" in C++14. This primarily intended to ease the creation of Niebloid-like function objects. The public header is:

An example of defining a function object using the macro can be seen below:

namespace foo {
namespace fancy_add_impl {
/// @brief A function object that adds two values.
class fancy_add {
public:
/// @brief Adds two values.
/// @tparam T The type of the values to add.
/// @param lhs The left-hand side of the addition.
/// @param rhs The right-hand side of the addition.
/// @return The result of the addition.
///
/// Note that we use the @c def and @c copydoc tags to copy the documentation of the overload and attach it to the
/// definition of the inline variable produced by the macro in order to make it display correctly in doxygen.
template <typename T>
constexpr auto operator()(T const& lhs, T const& rhs) const -> decltype(lhs + rhs) {
return lhs + rhs;
}
};
} // namespace fancy_add_impl
/// @def foo::fancy_add
/// @copydoc fancy_add_impl::fancy_add::operator()
ARENE_CPP14_INLINE_VARIABLE(fancy_add_impl::fancy_add, fancy_add);
} // namespace foo
Note
The type used with the macro must be constexpr default constructible.

Initializer-expression form

Some "inline variables" cannot be produced by default-constructing their type: for example, the result of arene::base::bind_back(callable, args...) captures the bound callable and arguments as state, so the same value cannot be re-created from the type alone. For these cases arene-base provides a companion macro that takes an initializer expression instead of a type:

ARENE_CPP14_INLINE_VARIABLE_INIT(name, init_expression)
#define ARENE_CPP14_INLINE_VARIABLE_INIT(name,...)
Emulates C++17's inline variable declarations for an arbitrary initializer expression.
Definition cpp14_inline.hpp:95

The deduced type is whatever init_expression evaluates to, just like a plain constexpr auto name = init_expression. The initializer is variadic so that expressions containing commas (e.g. template arguments of the constructed type) do not need extra parentheses at the call site.

In a >= C++17 context this expands to inline constexpr auto name = init_expression, which the standard guarantees is a single instance across translation units. In C++14 contexts the macro uses the variable-template + reference pattern documented in Defining compile-time constants for use in multiple translation units to obtain a single ODR-safe instance: an extern constexpr variable template tagged by std::nullptr_t is declared in a per-macro-invocation namespace, and a static constexpr reference in the enclosing namespace binds to that one instance so the trailing <> is hidden at use sites.

Unlike ARENE_CPP14_INLINE_VARIABLE this form does not require default-constructibility, but it does require that the initializer be a constant expression suitable for a constexpr variable.