From 4126ca650cb318f034420fac69b9101e16bc13c1 Mon Sep 17 00:00:00 2001 From: Adder Date: Tue, 7 Jan 2025 00:43:06 +0200 Subject: [PATCH] `integral_constant`: As per `std`: Add `operator()`. In the C++ Standard Library, `std::integral_constant` has `operator()`: https://timsong-cpp.github.io/cppwp/meta.help We hereby add `operator()` for the Boost version. --- .../boost/type_traits/integral_constant.hpp | 2 ++ test/integral_constant_test.cpp | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100755 test/integral_constant_test.cpp diff --git a/include/boost/type_traits/integral_constant.hpp b/include/boost/type_traits/integral_constant.hpp index 284ed02..7424c86 100644 --- a/include/boost/type_traits/integral_constant.hpp +++ b/include/boost/type_traits/integral_constant.hpp @@ -64,6 +64,7 @@ namespace boost{ return *static_cast*>(pdata); } BOOST_CONSTEXPR operator T()const BOOST_NOEXCEPT { return val; } + BOOST_CONSTEXPR T operator()()const BOOST_NOEXCEPT { return val; } }; template @@ -84,6 +85,7 @@ namespace boost{ return *static_cast*>(pdata); } BOOST_CONSTEXPR operator bool()const BOOST_NOEXCEPT { return val; } + BOOST_CONSTEXPR bool operator()()const BOOST_NOEXCEPT { return val; } }; template diff --git a/test/integral_constant_test.cpp b/test/integral_constant_test.cpp new file mode 100755 index 0000000..6938d5d --- /dev/null +++ b/test/integral_constant_test.cpp @@ -0,0 +1,28 @@ +// Copyright 2025 DoctorNoobingstoneIPresume +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include + +int main () +{ + // [2025-01-07] Adapted from example in https://en.cppreference.com/w/cpp/types/integral_constant ... + + using two_t = boost::integral_constant ; + using four_t = boost::integral_constant ; + + BOOST_STATIC_ASSERT ((! boost::is_same ::value)); + BOOST_STATIC_ASSERT ((two_t::value * 2 == four_t::value)); + BOOST_STATIC_ASSERT ((two_t () << 1 == four_t ())); + BOOST_STATIC_ASSERT ((two_t () () << 1 == four_t () ())); + + enum class E {e0, e1}; + using c0 = boost::integral_constant ; + using c1 = boost::integral_constant ; + BOOST_STATIC_ASSERT ((c0::value != E::e1)); + BOOST_STATIC_ASSERT ((c0 () == E::e0)); + BOOST_STATIC_ASSERT ((boost::is_same ::value)); +}