Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:53:21

0001 /* Copyright 2022 Joaquin M Lopez Munoz.
0002  * Distributed under the Boost Software License, Version 1.0.
0003  * (See accompanying file LICENSE_1_0.txt or copy at
0004  * http://www.boost.org/LICENSE_1_0.txt)
0005  *
0006  * See https://www.boost.org/libs/unordered for library home page.
0007  */
0008 
0009 #ifndef BOOST_UNORDERED_DETAIL_NARROW_CAST_HPP
0010 #define BOOST_UNORDERED_DETAIL_NARROW_CAST_HPP
0011 
0012 #include <boost/unordered/detail/static_assert.hpp>
0013 
0014 #include <boost/config.hpp>
0015 #include <type_traits>
0016 
0017 namespace boost{
0018 namespace unordered{
0019 namespace detail{
0020 
0021 template<typename To,typename From>
0022 constexpr To narrow_cast(From x) noexcept
0023 {
0024   BOOST_UNORDERED_STATIC_ASSERT(std::is_integral<From>::value);
0025   BOOST_UNORDERED_STATIC_ASSERT(std::is_integral<To>::value);
0026   BOOST_UNORDERED_STATIC_ASSERT(sizeof(From)>=sizeof(To));
0027 
0028   return static_cast<To>(
0029     x
0030 
0031 #if defined(__MSVC_RUNTIME_CHECKS)
0032     /* Avoids VS's "Run-Time Check Failure #1 - A cast to a smaller data type
0033      * has caused a loss of data."
0034      */
0035     &static_cast<typename std::make_unsigned<To>::type>(~static_cast<To>(0))
0036 #endif
0037   );
0038 }
0039 
0040 } /* namespace detail */
0041 } /* namespace unordered */
0042 } /* namespace boost */
0043 
0044 #endif