C++11 编写模板以选择更大的整数类型的方法

C++11 way to write template for picking bigger integer type?

本文关键字:整数 类型 方法 选择 C++11      更新时间:2023-10-16

在 C++11 的编译时,在一个接受 2 个模板参数的模板函数中,这两个参数都必须是无符号整数类型,我想让一个局部变量具有两个模板参数中任何一个具有更多位的类型。在 C++03 中,我可能会写这样的东西:

template<bool, class T, class U>
struct pick_first;
template<class T, class U>
struct pick_first<true, T, U> {
    typedef T type;
};
template<class T, class U>
struct pick_first<false, T, U> {
    typedef U type;
};
template<class T, class U>
struct pick_bigger {
    typedef typename pick_first<(sizeof(T) >= sizeof(U)), T, U>::type type;
};
// usage
template<class uintX_t, class uintY_t>
void foo() {
    typename pick_bigger<uintX_t, uintY_t>::type mylocal = 0;
    // insert doing stuff with mylocal here
}

我能否利用任何新的 C++11 功能来简化此操作?我知道我可以使用可变参数模板来使其不仅适用于对类型,而且无需使用pick_first我可以编写大量专用化来使其适用于新的int_leastX_t和int_fastX_t类型。但我很好奇是否有一种更好的方法。也许以某种方式利用auto/constexpr/decltype?

你的pick_first在C++11中只是std::condition,所以你可以写

template<class T, class U>
struct wider {
    using type = typename std::conditional<sizeof(T) >= sizeof(U), T, U>::type; // I'm using the C++11 type alias feature 1) to educate people about them and 2) because I like them better than typedefs.
};

如果你只想要一个适合保存涉及两种类型的表达式结果的类型,并且不一定需要两种类型中的一种,那么std::common_type,或者也许是auto,是最好的解决方案:

template<class uintX_t, class uintY_t>
void foo() {
    typename std::common_type<uintX_t, uintY_t>::type mylocal = 0;
    // insert doing stuff with mylocal here
}
// or
template<class uintX_t, class uintY_t>
void foo(uintX_t x, uintY_t y) {
    auto mylocal = x + y;
}

并且您对pick_bigger的实现缺少一个typenametypedef typename pick_first<(sizeof(T) >= sizeof(U)), T, U>::type type;

这是我

的解决方案,可以从N种类型中选择最广泛的类型,直到模板递归限制。

我还包含了最窄的代码。

template <typename TFirst, typename... TOther>
struct widest {
private:
  using rhs_recursive_type = typename widest<TOther...>::type;
public:
  using type =
      typename std::conditional<sizeof(TFirst) >= sizeof(rhs_recursive_type),
                                TFirst, rhs_recursive_type>::type;
};
template <typename TFirst>
struct widest<TFirst> {
  using type = TFirst;
};
template <typename TFirst, typename... TOther>
struct narrowest {
private:
  using rhs_recursive_type = typename widest<TOther...>::type;
public:
  using type =
      typename std::conditional<sizeof(TFirst) <= sizeof(rhs_recursive_type),
                                TFirst, rhs_recursive_type>::type;
};
template <typename TFirst>
struct narrowest<TFirst> {
  using type = TFirst;
};

由于两种类型都是无符号的,因此只需执行decltype( T1() + T2() ) 即可。