从<number of bytes>和<signedness>

问题描述 投票:0回答:3
  • 我有一个
    constexpr size_t byte_count
    ,它是1、2、4或8
  • 我有一个
    constexpr bool is_signed
    ,这显然不是对就是错

我想用

typedef
using
为整数类型
T
构造一个
sizeof(T) == byte_count
/
std::is_signed_v<T> == is_signed

  1. <type_traits>
    (或标准库中的其他地方)是否有这样做的东西?

  2. 否则,最紧凑的实现方式是什么?

  • 我真的很喜欢不对类型进行硬编码的简短内容,因为它将在多个地方使用,在这些地方引入辅助函数/类型很烦人(它在生成的代码中......叹息)。

  • 我没有动力。

我目前的解决方案:

#include <type_traits>
#include <tuple>
#include <cstddef>
#include <cstdint>

int main(){
    constexpr size_t byte_count = 4; // either 1, 2, 4, or 8
    constexpr bool is_signed = true; // either true or false

    // I would like to have something shorter and less hardcoded than this
    using T = std::conditional_t<
        is_signed,
        std::tuple_element_t<byte_count, std::tuple<int8_t, int16_t, int16_t, int32_t, int32_t, int32_t, int32_t, int64_t>>,
        std::tuple_element_t<byte_count, std::tuple<uint8_t, uint16_t, uint16_t, uint32_t, uint32_t, uint32_t, uint32_t, uint64_t>>
    >;

    static_assert(sizeof(T) == byte_count);
    static_assert(std::is_signed_v<T> == is_signed);
}
c++ templates std typetraits
3个回答
6
投票

不幸的是,您将不得不自己实现映射。

您可以使用

std::make_signed_t
摆脱一半的案件。

像这样的东西:

template<std::size_t ByteWidth>
struct select_uint;

template<> struct select_uint<1> { using type = std::uint8_t; };
template<> struct select_uint<2> { using type = std::uint16_t; };
template<> struct select_uint<4> { using type = std::uint32_t; };
template<> struct select_uint<8> { using type = std::uint64_t; };

template<std::size_t ByteWidth, bool IsSigned>
using select_int_t = std::conditional_t<IsSigned, typename select_uint<ByteWidth>::type, std::make_signed_t<typename select_uint<ByteWidth>::type>>;

1
投票

我认为没有比明确列出所有类型更好的了。

这可能看起来像:

template <int size, bool is_signed>
struct int_traits;

template <>
struct int_traits<1, false> { using type = uint8_t; };
template <>
struct int_traits<1, true> { using type = int8_t; };
template <>
struct int_traits<2, false> { using type = uint16_t; };
template <>
struct int_traits<2, true> { using type = int16_t; };

template <int size, bool is_signed>
using int_t = int_traits<size, is_signed>::type;

int main()
{
    int_t<1, true> b = 1;
}

1
投票

您将不得不列出类型,但您可以使用辅助函数来简化此操作。这可能看起来像

template <std::size_t bytes, bool is_signed>
auto int_type()
{
    if constexpr (bytes == 1 && is_signed)       return int8_t{};
    else if constexpr (bytes == 1)               return uint8_t{};
    else if constexpr (bytes == 2 && is_signed)  return int16_t{};
    else if constexpr (bytes == 2)               return uint16_t{};
    else if constexpr (bytes == 4 && is_signed)  return int32_t{};
    else if constexpr (bytes == 4)               return uint32_t{};
    else if constexpr (bytes == 8 && is_signed)  return int64_t{};
    else if constexpr (bytes == 8)               return uint64_t{};
    else                                         return; // void for the error case
}

template <std::size_t bytes, bool is_signed>
using int_type_t = decltype(int_type<bytes, is_signed>());

int main()
{
    int_type_t<1, true> b = 1;
}
© www.soinside.com 2019 - 2024. All rights reserved.