mirror of
https://github.com/boostorg/hana.git
synced 2026-01-21 17:02:25 +00:00
[core] Add embedding from char to any integral type with proper signedness
This commit is contained in:
@@ -107,12 +107,40 @@ BOOST_HANA_NAMESPACE_BEGIN
|
||||
BOOST_HANA_DEFINE_EMBEDDING_IMPL(unsigned int , unsigned short);
|
||||
BOOST_HANA_DEFINE_EMBEDDING_IMPL(unsigned int , unsigned char);
|
||||
BOOST_HANA_DEFINE_EMBEDDING_IMPL(unsigned short , unsigned char);
|
||||
#undef BOOST_HANA_DEFINE_EMBEDDING_IMPL
|
||||
|
||||
namespace detail {
|
||||
template <typename T>
|
||||
struct copy_char_signedness {
|
||||
using type = typename std::conditional<std::is_signed<char>::value,
|
||||
std::make_signed<T>, std::make_unsigned<T>
|
||||
>::type::type;
|
||||
};
|
||||
}
|
||||
|
||||
// If `char` is signed, we define an embedding from `char` to any signed
|
||||
// integral type. Otherwise, we define one from `char` to any unsigned
|
||||
// integral type.
|
||||
#define BOOST_HANA_DEFINE_CHAR_EMBEDDING_IMPL(TO) \
|
||||
template <> \
|
||||
struct to_impl<detail::copy_char_signedness<TO>::type, char> \
|
||||
: embedding<> \
|
||||
{ \
|
||||
static constexpr detail::copy_char_signedness<TO>::type \
|
||||
apply(char x) \
|
||||
{ return x; } \
|
||||
} \
|
||||
/**/
|
||||
BOOST_HANA_DEFINE_CHAR_EMBEDDING_IMPL(long long);
|
||||
BOOST_HANA_DEFINE_CHAR_EMBEDDING_IMPL(long);
|
||||
BOOST_HANA_DEFINE_CHAR_EMBEDDING_IMPL(int);
|
||||
BOOST_HANA_DEFINE_CHAR_EMBEDDING_IMPL(short);
|
||||
#undef BOOST_HANA_DEFINE_CHAR_EMBEDDING_IMPL
|
||||
|
||||
template <typename T>
|
||||
struct to_impl<T*, decltype(nullptr)> : embedding<> {
|
||||
static constexpr T* apply(decltype(nullptr)) { return nullptr; }
|
||||
};
|
||||
#undef BOOST_HANA_DEFINE_EMBEDDING_IMPL
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////
|
||||
// is_convertible
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#include <boost/hana/core/to.hpp>
|
||||
#include <boost/hana/equal.hpp>
|
||||
|
||||
#include <climits>
|
||||
namespace hana = boost::hana;
|
||||
|
||||
|
||||
@@ -35,4 +38,25 @@ static_assert(hana::is_embedded<unsigned short, unsigned int>{}, "");
|
||||
static_assert(hana::is_embedded<unsigned char, unsigned int>{}, "");
|
||||
static_assert(hana::is_embedded<unsigned char, unsigned short>{}, "");
|
||||
|
||||
#if CHAR_MIN < 0 // char is signed
|
||||
|
||||
static_assert(hana::is_embedded<char, signed long long>{}, "");
|
||||
static_assert(hana::is_embedded<char, signed long>{}, "");
|
||||
static_assert(hana::is_embedded<char, signed int>{}, "");
|
||||
static_assert(hana::is_embedded<char, signed short>{}, "");
|
||||
|
||||
static_assert(hana::equal('a', static_cast<signed int>('a')), "");
|
||||
|
||||
#else // char is unsigned
|
||||
|
||||
static_assert(hana::is_embedded<char, unsigned long long>{}, "");
|
||||
static_assert(hana::is_embedded<char, unsigned long>{}, "");
|
||||
static_assert(hana::is_embedded<char, unsigned int>{}, "");
|
||||
static_assert(hana::is_embedded<char, unsigned short>{}, "");
|
||||
|
||||
static_assert(hana::equal('a', static_cast<unsigned int>('a')), "");
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
int main() { }
|
||||
|
||||
Reference in New Issue
Block a user