1/* Copyright 2022 Joaquin M Lopez Munoz.
2 * Distributed under the Boost Software License, Version 1.0.
3 * (See accompanying file LICENSE_1_0.txt or copy at
4 * http://www.boost.org/LICENSE_1_0.txt)
5 *
6 * See https://www.boost.org/libs/unordered for library home page.
7 */
8
9#ifndef BOOST_UNORDERED_DETAIL_NARROW_CAST_HPP
10#define BOOST_UNORDERED_DETAIL_NARROW_CAST_HPP
11
12#include <boost/unordered/detail/static_assert.hpp>
13
14#include <boost/config.hpp>
15#include <type_traits>
16
17namespace boost{
18namespace unordered{
19namespace detail{
20
21template<typename To,typename From>
22constexpr To narrow_cast(From x) noexcept
23{
24 BOOST_UNORDERED_STATIC_ASSERT(std::is_integral<From>::value);
25 BOOST_UNORDERED_STATIC_ASSERT(std::is_integral<To>::value);
26 BOOST_UNORDERED_STATIC_ASSERT(sizeof(From)>=sizeof(To));
27
28 return static_cast<To>(
29 x
30
31#if defined(__MSVC_RUNTIME_CHECKS)
32 /* Avoids VS's "Run-Time Check Failure #1 - A cast to a smaller data type
33 * has caused a loss of data."
34 */
35 &static_cast<typename std::make_unsigned<To>::type>(~static_cast<To>(0))
36#endif
37 );
38}
39
40} /* namespace detail */
41} /* namespace unordered */
42} /* namespace boost */
43
44#endif
45

source code of boost/libs/unordered/include/boost/unordered/detail/narrow_cast.hpp