Files
boost_unordered/include/boost/unordered/detail/narrow_cast.hpp
T

44 lines
1.1 KiB
C++
Raw Normal View History

2022-12-05 11:50:18 +01:00
/* Copyright 2022 Joaquin M Lopez Munoz.
* 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)
*
* See https://www.boost.org/libs/unordered for library home page.
*/
#ifndef BOOST_UNORDERED_DETAIL_NARROW_CAST_HPP
#define BOOST_UNORDERED_DETAIL_NARROW_CAST_HPP
2023-09-01 15:03:49 -07:00
#include <boost/unordered/detail/static_assert.hpp>
2022-12-05 11:50:18 +01:00
#include <boost/config.hpp>
2023-09-20 12:57:07 -07:00
#include <type_traits>
2022-12-05 11:50:18 +01:00
namespace boost{
namespace unordered{
namespace detail{
template<typename To,typename From>
2023-08-28 12:48:03 -07:00
constexpr To narrow_cast(From x) noexcept
2022-12-05 11:50:18 +01:00
{
2023-09-20 12:57:07 -07:00
BOOST_UNORDERED_STATIC_ASSERT(std::is_integral<From>::value);
BOOST_UNORDERED_STATIC_ASSERT(std::is_integral<To>::value);
2023-09-01 15:03:49 -07:00
BOOST_UNORDERED_STATIC_ASSERT(sizeof(From)>=sizeof(To));
2022-12-05 11:50:18 +01:00
return static_cast<To>(
x
#if defined(__MSVC_RUNTIME_CHECKS)
/* Avoids VS's "Run-Time Check Failure #1 - A cast to a smaller data type
* has caused a loss of data."
*/
2023-09-20 12:57:07 -07:00
&static_cast<typename std::make_unsigned<To>::type>(~static_cast<To>(0))
2022-12-05 11:50:18 +01:00
#endif
);
}
} /* namespace detail */
} /* namespace unordered */
} /* namespace boost */
#endif