From 71d24224a7e63cbea0329c3b02c63c45d73cd015 Mon Sep 17 00:00:00 2001 From: Nana Sakisaka <1901813+saki7@users.noreply.github.com> Date: Tue, 18 Aug 2026 22:14:58 +0900 Subject: [PATCH] Add `make_integer_of_size`, `make_(signed|unsigned)_of_size` --- include/iris/bits/is_function_object.hpp | 2 + include/iris/bits/specialization_of.hpp | 2 + include/iris/requirements.hpp | 4 +- include/iris/stdint.hpp | 130 +++++++++++++++++++++++ include/iris/type_traits.hpp | 41 ++++++- test/CMakeLists.txt | 1 + test/stdint.cpp | 118 ++++++++++++++++++++ 7 files changed, 295 insertions(+), 3 deletions(-) create mode 100644 include/iris/stdint.hpp create mode 100644 test/stdint.cpp diff --git a/include/iris/bits/is_function_object.hpp b/include/iris/bits/is_function_object.hpp index 420aaa0..59c2788 100644 --- a/include/iris/bits/is_function_object.hpp +++ b/include/iris/bits/is_function_object.hpp @@ -3,6 +3,8 @@ // SPDX-License-Identifier: MIT +#include // IWYU pragma: keep + #include namespace iris { diff --git a/include/iris/bits/specialization_of.hpp b/include/iris/bits/specialization_of.hpp index 43d5609..b9a771c 100644 --- a/include/iris/bits/specialization_of.hpp +++ b/include/iris/bits/specialization_of.hpp @@ -3,6 +3,8 @@ // SPDX-License-Identifier: MIT +#include // IWYU pragma: keep + #include namespace iris { diff --git a/include/iris/requirements.hpp b/include/iris/requirements.hpp index 16be80e..6a43bda 100644 --- a/include/iris/requirements.hpp +++ b/include/iris/requirements.hpp @@ -3,11 +3,13 @@ // SPDX-License-Identifier: MIT +#include // IWYU pragma: keep + #include #include #include -#include +#include // IWYU pragma: export #include #include diff --git a/include/iris/stdint.hpp b/include/iris/stdint.hpp new file mode 100644 index 0000000..1edf9e4 --- /dev/null +++ b/include/iris/stdint.hpp @@ -0,0 +1,130 @@ +#ifndef IRIS_ZZ_STDINT_HPP +#define IRIS_ZZ_STDINT_HPP + +// SPDX-License-Identifier: MIT + +#include // IWYU pragma: keep + +#include + +#include +#include + +#include // IWYU pragma: export +#include // IWYU pragma: keep + +namespace iris { + +// `std::make_signed` family requires T to be nonbool integer or enum + +namespace detail { + +template +struct integer_of_size_impl; + +template<> +struct integer_of_size_impl +{ + using type = std::int8_t; +}; + +template<> +struct integer_of_size_impl +{ + using type = std::int16_t; +}; + +template<> +struct integer_of_size_impl +{ + using type = std::int32_t; +}; + +template<> +struct integer_of_size_impl +{ + using type = std::int64_t; +}; + +// ---------------------------------------------- + +template<> +struct integer_of_size_impl +{ + using type = std::uint8_t; +}; + +template<> +struct integer_of_size_impl +{ + using type = std::uint16_t; +}; + +template<> +struct integer_of_size_impl +{ + using type = std::uint32_t; +}; + +template<> +struct integer_of_size_impl +{ + using type = std::uint64_t; +}; + +template +struct signedness_of_integral; + +template +struct signedness_of_integral : std::bool_constant> +{}; + +template + requires std::is_enum_v +struct signedness_of_integral : std::bool_constant>> +{}; + +template +using make_signed_of_size_impl_t = integer_of_size_impl::type; + +template +using make_unsigned_of_size_impl_t = integer_of_size_impl::type; + +template +using make_integer_of_size_impl_t = integer_of_size_impl::value, sizeof(T)>::type; + +} // detail + +template +struct make_signed_of_size +{ + static_assert(std::integral && !std::same_as, bool> || std::is_enum_v); + using type = remove_cv::template apply; +}; + +template +using make_signed_of_size_t = make_signed_of_size::type; + +template +struct make_unsigned_of_size +{ + static_assert(std::integral && !std::same_as, bool> || std::is_enum_v); + using type = remove_cv::template apply; +}; + +template +using make_unsigned_of_size_t = make_unsigned_of_size::type; + +template +struct make_integer_of_size +{ + static_assert(std::integral && !std::same_as, bool> || std::is_enum_v); + using type = remove_cv::template apply; +}; + +template +using make_integer_of_size_t = make_integer_of_size::type; + +} // iris + +#endif diff --git a/include/iris/type_traits.hpp b/include/iris/type_traits.hpp index 7400713..fcce5ad 100644 --- a/include/iris/type_traits.hpp +++ b/include/iris/type_traits.hpp @@ -3,7 +3,7 @@ // SPDX-License-Identifier: MIT -#include +#include // IWYU pragma: keep #include // IWYU pragma: export #include // IWYU pragma: export @@ -11,13 +11,50 @@ #include #include -#include +#include // IWYU pragma: export #include #include namespace iris { +template +struct remove_cv +{ + using type = T; + + template