[libcxx] r228704 - [libcxx] Properly convert the count arguments to the *_n algorithms before use.

Agustín K-ballo Bergé kaballo86 at hotmail.com
Wed Feb 11 06:08:30 PST 2015


On 2/10/2015 1:46 PM, Eric Fiselier wrote:
> Author: ericwf
> Date: Tue Feb 10 10:46:42 2015
> New Revision: 228704
>
> URL: http://llvm.org/viewvc/llvm-project?rev=228704&view=rev
> Log:
> [libcxx] Properly convert the count arguments to the *_n algorithms before use.
>
> Summary:
> The requirement on the `Size` type passed to *_n algorithms is that it is convertible to an integral type. This means we can't use a variable of type `Size` directly. Instead we need to convert it to an integral type first.  The problem is finding out what integral type to convert it to.  `__convert_to_integral` figures out what integral type to convert it to and performs the conversion, It also promotes the resulting integral type so that it is at least as big as an integer. `__convert_to_integral` also has a special case for converting enums. This should only work on non-scoped enumerations because it does not apply an explicit conversion from the enum to its underlying type.

[snip]

> Modified: libcxx/trunk/include/type_traits
> URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=228704&r1=228703&r2=228704&view=diff
> ==============================================================================
> --- libcxx/trunk/include/type_traits (original)
> +++ libcxx/trunk/include/type_traits Tue Feb 10 10:46:42 2015
> @@ -3646,6 +3646,48 @@ struct underlying_type
>
>   #endif // _LIBCPP_UNDERLYING_TYPE
>
> +
> +template <class _Tp, bool = std::is_enum<_Tp>::value>
> +struct __sfinae_underlying_type
> +{
> +    typedef typename underlying_type<_Tp>::type type;
> +    typedef decltype(((type)1) + 0) __promoted_type;
> +};
> +
> +template <class _Tp>
> +struct __sfinae_underlying_type<_Tp, false> {};
> +
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +int __convert_to_integral(int __val) { return __val; }

These combinations of `_LIBCPP_INLINE_VISIBILITY` 
`_LIBCPP_ALWAYS_INLINE` trigger a `__forceinline` used more than once 
warning on MSVC. Isn't this redundant? I don't care about the warning if 
the code is actually intended.

> +
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +unsigned __convert_to_integral(unsigned __val) { return __val; }
> +
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +long __convert_to_integral(long __val) { return __val; }
> +
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +unsigned long __convert_to_integral(unsigned long __val) { return __val; }
> +
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +long long __convert_to_integral(long long __val) { return __val; }
> +
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +unsigned long long __convert_to_integral(unsigned long long __val) {return __val; }
> +
> +#ifndef _LIBCPP_HAS_NO_INT128
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +__int128_t __convert_to_integral(__int128_t __val) { return __val; }
> +
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +__uint128_t __convert_to_integral(__uint128_t __val) { return __val; }
> +#endif
> +
> +template <class _Tp>
> +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_ALWAYS_INLINE
> +typename __sfinae_underlying_type<_Tp>::__promoted_type
> +__convert_to_integral(_Tp __val) { return __val; }
> +
>   #ifndef _LIBCPP_HAS_NO_ADVANCED_SFINAE
>
>   template <class _Tp>
>

[more-snip]

Regards,
-- 
Agustín K-ballo Bergé.-
http://talesofcpp.fusionfenix.com




More information about the cfe-commits mailing list