From 9aaa4edda19a201f393d6eac457c4c484c5aab16 Mon Sep 17 00:00:00 2001 From: jzmaddock Date: Fri, 24 Jan 2020 18:33:21 +0000 Subject: [PATCH] Add helper macro BOOST_IF_CONSTEXPR. --- doc/macro_reference.qbk | 1 + include/boost/config/detail/suffix.hpp | 8 ++++++++ test/helper_macro_test.cpp | 11 +++++++++++ 3 files changed, 20 insertions(+) diff --git a/doc/macro_reference.qbk b/doc/macro_reference.qbk index 277b9d36..75c1dc9f 100644 --- a/doc/macro_reference.qbk +++ b/doc/macro_reference.qbk @@ -978,6 +978,7 @@ provide compliant C++17 support. [table [[Macro ][ Description ]] [[`BOOST_INLINE_VARIABLE`][This macro expands to `inline` on compilers that support C++17 inline variables and to nothing otherwise. Users may need to check for `BOOST_NO_CXX17_INLINE_VARIABLES` for further adjustments to the code.]] +[[`BOOST_IF_CONSTEXPR`][Expands to `if constexpr` when supported, or `if` otherwise.]] ] [endsect] diff --git a/include/boost/config/detail/suffix.hpp b/include/boost/config/detail/suffix.hpp index 9131cba4..4dfb0422 100644 --- a/include/boost/config/detail/suffix.hpp +++ b/include/boost/config/detail/suffix.hpp @@ -1002,6 +1002,14 @@ namespace std{ using ::type_info; } #else #define BOOST_INLINE_VARIABLE #endif +// +// C++17 if constexpr +// +#if !defined(BOOST_NO_CXX17_IF_CONSTEXPR) +# define BOOST_IF_CONSTEXPR if constexpr +#else +# define BOOST_IF_CONSTEXPR if +#endif // // Unused variable/typedef workarounds: diff --git a/test/helper_macro_test.cpp b/test/helper_macro_test.cpp index efdb20f9..18b9268a 100644 --- a/test/helper_macro_test.cpp +++ b/test/helper_macro_test.cpp @@ -51,6 +51,12 @@ struct no_unique BOOST_ATTRIBUTE_NO_UNIQUE_ADDRESS empty b; }; +template +struct trait +{ + enum { value = b }; +}; + int main() { @@ -66,6 +72,11 @@ int main() always_throw(); nodiscard_struct s; no_unique no_un; + + BOOST_IF_CONSTEXPR(trait::value) + { + result += 2; + } } catch(int) {