Merge pull request #198 from boostorg/pr181

Integrate PR181
This commit is contained in:
jzmaddock
2025-04-21 15:19:26 +01:00
committed by GitHub
2 changed files with 39 additions and 6 deletions

View File

@ -60,10 +60,11 @@ namespace boost{
operator const mpl::integral_c<T, val>& ()const
{
static const char data[sizeof(long)] = { 0 };
static const void* pdata = data;
return *(reinterpret_cast<const mpl::integral_c<T, val>*>(pdata));
const void* const pdata = data;
return *static_cast<const mpl::integral_c<T, val>*>(pdata);
}
BOOST_CONSTEXPR operator T()const { return val; }
BOOST_CONSTEXPR operator T()const BOOST_NOEXCEPT { return val; }
BOOST_CONSTEXPR T operator()()const BOOST_NOEXCEPT { return val; }
};
template <class T, T val>
@ -80,10 +81,11 @@ namespace boost{
operator const mpl::bool_<val>& ()const
{
static const char data[sizeof(long)] = { 0 };
static const void* pdata = data;
return *(reinterpret_cast<const mpl::bool_<val>*>(pdata));
const void* const pdata = data;
return *static_cast<const mpl::bool_<val>*>(pdata);
}
BOOST_CONSTEXPR operator bool()const { return val; }
BOOST_CONSTEXPR operator bool()const BOOST_NOEXCEPT { return val; }
BOOST_CONSTEXPR bool operator()()const BOOST_NOEXCEPT { return val; }
};
template <bool val>

31
test/integral_constant_test.cpp Executable file
View File

@ -0,0 +1,31 @@
// 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 <boost/type_traits/integral_constant.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/static_assert.hpp>
int main ()
{
// [2025-01-07] Adapted from example in https://en.cppreference.com/w/cpp/types/integral_constant ...
#ifndef BOOST_NO_CXX11_CONSTEXPR
using two_t = boost::integral_constant <int, 2>;
using four_t = boost::integral_constant <int, 4>;
BOOST_STATIC_ASSERT ((! boost::is_same <two_t, four_t>::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 <E, E::e0>;
using c1 = boost::integral_constant <E, E::e1>;
BOOST_STATIC_ASSERT ((c0::value != E::e1));
BOOST_STATIC_ASSERT ((c0 () == E::e0));
BOOST_STATIC_ASSERT ((boost::is_same <c1, c1>::value));
#endif
}