69 lines · c
1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H10#define _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H11 12#include <__config>13#include <__type_traits/enable_if.h>14#include <__type_traits/is_enum.h>15#include <__type_traits/is_floating_point.h>16#include <__type_traits/underlying_type.h>17 18#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)19# pragma GCC system_header20#endif21 22_LIBCPP_BEGIN_NAMESPACE_STD23 24inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR int __convert_to_integral(int __val) { return __val; }25 26inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unsigned __convert_to_integral(unsigned __val) { return __val; }27 28inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long __convert_to_integral(long __val) { return __val; }29 30inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unsigned long __convert_to_integral(unsigned long __val) {31 return __val;32}33 34inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long long __convert_to_integral(long long __val) { return __val; }35 36inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR unsigned long long __convert_to_integral(unsigned long long __val) {37 return __val;38}39 40template <typename _Fp, __enable_if_t<is_floating_point<_Fp>::value, int> = 0>41inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR long long __convert_to_integral(_Fp __val) {42 return __val;43}44 45#if _LIBCPP_HAS_INT12846inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __int128_t __convert_to_integral(__int128_t __val) { return __val; }47 48inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR __uint128_t __convert_to_integral(__uint128_t __val) { return __val; }49#endif50 51template <class _Tp, bool = is_enum<_Tp>::value>52struct __sfinae_underlying_type {53 using type = __underlying_type_t<_Tp>;54 typedef decltype(((type)1) + 0) __promoted_type;55};56 57template <class _Tp>58struct __sfinae_underlying_type<_Tp, false> {};59 60template <class _Tp>61inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename __sfinae_underlying_type<_Tp>::__promoted_type62__convert_to_integral(_Tp __val) {63 return __val;64}65 66_LIBCPP_END_NAMESPACE_STD67 68#endif // _LIBCPP___UTILITY_CONVERT_TO_INTEGRAL_H69