From 87b7059a1212adbaa8e147f7a907caa35929051b Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Thu, 20 Jun 2024 09:41:43 -0400 Subject: [PATCH] Avoid colliding with macro names `B1` and `PI` This helps Arduino compatibility. Of course, this whole situation is an excellent case in point to illustrate why, if you _must_ use macros, _never_ to give them such easily collidable names. But that is not the users' fault, so since we can work around the problem, we do so. The `B1` template parameter can be renamed to `B` without loss of readability. `PI` is a little more challenging. We do use it in one place in our code, in defining `Degrees`, so we must replace that with its definition of `Magnitude{}`. We provide workaround instructions in the comments. We leave alone the many instances of `PI` in test files, since that shouldn't affect these users. --- au/magnitude.hh | 9 +++++++++ au/stdx/type_traits.hh | 16 ++++++++-------- au/units/degrees.hh | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/au/magnitude.hh b/au/magnitude.hh index c0a585dc..26fafb50 100644 --- a/au/magnitude.hh +++ b/au/magnitude.hh @@ -131,7 +131,16 @@ using CommonMagnitudeT = typename CommonMagnitude::type; // Value based interface for Magnitude. static constexpr auto ONE = Magnitude<>{}; + +#ifndef PI +// Some users must work with frameworks that define `PI` as a macro. Having a macro with this +// easily collidable name is exceedingly unwise. Nevertheless, that's not the users' fault, so we +// accommodate those frameworks by omitting the definition of `PI` in this case. +// +// If you are stuck with such a framework, you can choose a different name that does not collide, +// and reproduce the following line in your own system. static constexpr auto PI = Magnitude{}; +#endif template constexpr auto operator*(Magnitude, Magnitude) { diff --git a/au/stdx/type_traits.hh b/au/stdx/type_traits.hh index ae28e4a1..db4402b7 100644 --- a/au/stdx/type_traits.hh +++ b/au/stdx/type_traits.hh @@ -32,18 +32,18 @@ using bool_constant = std::integral_constant; // Source: adapted from (https://en.cppreference.com/w/cpp/types/conjunction). template struct conjunction : std::true_type {}; -template -struct conjunction : B1 {}; -template -struct conjunction : std::conditional_t, B1> {}; +template +struct conjunction : B {}; +template +struct conjunction : std::conditional_t, B> {}; // Source: adapted from (https://en.cppreference.com/w/cpp/types/disjunction). template struct disjunction : std::false_type {}; -template -struct disjunction : B1 {}; -template -struct disjunction : std::conditional_t> {}; +template +struct disjunction : B {}; +template +struct disjunction : std::conditional_t> {}; // Source: adapted from (https://en.cppreference.com/w/cpp/types/negation). template diff --git a/au/units/degrees.hh b/au/units/degrees.hh index b32632e9..333e8c06 100644 --- a/au/units/degrees.hh +++ b/au/units/degrees.hh @@ -28,7 +28,7 @@ struct DegreesLabel { }; template constexpr const char DegreesLabel::label[]; -struct Degrees : decltype(Radians{} * PI / mag<180>()), DegreesLabel { +struct Degrees : decltype(Radians{} * Magnitude{} / mag<180>()), DegreesLabel { using DegreesLabel::label; }; constexpr auto degree = SingularNameFor{};