根据模板整数参数选择整数类型

时间:2015-07-10 06:52:06

标签: c++ templates

我想创建一个类模板,它接受一个无符号整数参数,并且有一个成员u_,其类型是保存整数参数的最小无符号整数类型。

所以:

template <uint64_t k>
class A {
  ??? u_;
};

对于A<0>u_应为uint8_t类型。 A<255>也是如此。对于A<256>u_应为uint16_t类型等。

你会如何实现这个?

6 个答案:

答案 0 :(得分:10)

这段元编程技巧实现了它:

template<unsigned int Y> struct typed
{
    typedef typename typed<(Y & (Y - 1)) == 0 ? Y / 2 : (Y & (Y - 1))>::type type;
};

template<> struct typed<0>
{
    typedef std::uint8_t type;
};

template<> struct typed<256>
{
    typedef std::uint16_t type;
};

template<> struct typed<65536>
{
    typedef std::uint32_t type;
};

/* ToDo - add more specialisations as necessary*/

template<unsigned k> class A
{
public:
    unsigned static const k_ = k; /*constexpr if your compiler supports it*/
    typename typed<k>::type u_;
};

用法完全在问题中。

非专业化模板版本采用先前的类型。 typed<0>阻止静态递归。其他专业作为适当类型的锚点。

编译时可评估(Y & (Y - 1)) == 0 ? Y / 2 : (Y & (Y - 1))通过删除Y的最右位直到达到2的幂来减少实例化的数量,然后在此之后除以2。 (Acknowledge @ Jarod42)。

答案 1 :(得分:8)

使用C ++ 11,您可以使用std::conditional

#include <cassert>
#include <cstdint>
#include <limits>
#include <type_traits>

template<std::uint32_t K>
class A
{
public:
  decltype(K) k_ = K;

  typename std::conditional<K <= UINT8_MAX,
                            std::uint8_t,
                            typename std::conditional<K <= UINT16_MAX,
                            std::uint16_t,
                            std::uint32_t>::type>::type u_;
};

int main()
{
  A<100> small;
  A<1000> medium;
  A<100000> large;

  assert( (std::is_same<std::uint8_t, decltype(small.u_)>::value) );
  assert( (std::is_same<std::uint16_t, decltype(medium.u_)>::value) );
  assert( (std::is_same<std::uint32_t, decltype(large.u_)>::value) );
}

这假定:

    {li> uint8_t template<uint8_t k, typename >只是一种疏忽,或者正如Aaron McDaid的评论所指出的那样,这个例子不起作用。我将uint8_t更改为uint32_t(示例可以平滑地扩展为uint64_t);
  • int k_ = k;in-class member initialization。对于常量定义,您可以使用enum {k_ = K};
  • u_中的
  • typename u_;是数据成员。如果是类型定义,您可以使用typedeftype-alias(C ++ 11)轻松更改示例。

如果您不能使用C ++ 11,那么boost::conditional或者您可以编写自己的版本:

template<bool, class T, class F>
struct conditional { typedef T type; };

template<class T, class F>
struct conditional<false, T, F> { typedef F type; }; 

答案 2 :(得分:4)

如果我们想要的是:给定一个uint64_t模板参数,给出能够表示它的最小无符号类型,那么我们真正想要的只是编译时的一个简单迭代。

namespace details {
    template <typename T>
    struct tag {
        using type = T;
    };

    // base case: just fail
    template <uint64_t V, typename... >
    struct min_unsigned_type;

    // recursive case: check using numeric_limits
    template <uint64_t V, typename T, typename... Ts>
    struct min_unsigned_type<V, T, Ts...>
    : std::conditional_t<(V <= std::numeric_limits<T>::max()),
                         tag<T>,
                         min_unsigned_type<V, Ts...>>
    { };
}

然后只是一个将事物包装在一起的别名:

template <uint64_t V>
using min_unsigned_type = 
    typename details::min_unsigned_type<V, 
        uint8_t, uint16_t, uint32_t, uint64_t>::type;

这具有额外的优势,即能够轻松指定您想要走多远,或者甚至能够添加更大的无符号类型(如果您认为必要的话)。

最后你的班级:

template <uint64_t V>
struct A {
    static constexpr uint64_t k_ = V;
    min_unsigned_type<V> u_;
};

答案 3 :(得分:0)

您可以对部分绑定的模板使用声明:

template<uint8_t k>
using A_uint8 = A<k, uint8_t>;

template<uint8_t k>
using A_uint16 = A<k, uint16_t>;

然后:

A_uint8<7> hello; // -> A<7, uint8_t>;
A_uint16<256> hello; // -> A<256, uint16_t>;

答案 4 :(得分:0)

template<uintmax_t Id>
struct A;

template<>
struct A<0>{
    enum {id = 0};
    using type = uint8_t;
};

template<>
struct A<255>{
    enum {id = 255};
    using type = uint8_t;
};

template<>
struct A<256>{
    enum {id = 256};
    using type = uint16_t;
};

int main(){
    typename A<255>::type a0 = 255; // uint8_t
    typename A<256>::type a1 = 256; // uint16_t
}

但我认为你想要一个类,以便256以下的每个值都定义uint8_t,65536以下的每个值都会定义uint16_t等等。

所以,只要这样,你就是这样做的:

template<uintmax_t Value>
struct A{
    enum {value = Value};
    using type = std::conditional_t<
        (Value <= std::numeric_limits<uint8_t>::max()), uint8_t,
            std::conditional_t<
                (Value <= std::numeric_limits<uint16_t>::max()), uint16_t,
                    std::conditional_t<
                        (Value <= std::numeric_limits<uint32_t>::max()), uint32_t
                            std::conditional_t<
                                (Value <= std::numeric_limits<uint64_t>::max()), uint64_t, uintmax_t
                            >
                    >
            >
    >;
};

And here's a live example

答案 5 :(得分:-2)

非常感谢。你们好棒! 顺便说一下,我正在使用:

typedef typename std::conditional<k <= UINT8_MAX,
                        std::uint8_t,
                        typename std::conditional<k <= UINT16_MAX,
                        std::uint16_t,
                        typename std::conditional<k <= UINT32_MAX,
                        std::uint32_t,
                        std::uint64_t>::type>::type>::type TypeU;

std::pair<TypeU, std::shared_ptr<ProtoData>> min_;
std::pair<TypeU, std::shared_ptr<ProtoData>> max_;