是否有大小为模板参数的标准整数类型?

Are there standard integer types with sizes being template parameters?

假设我需要制作一个模板,其成员的长度恰好为 N 位,其中 N 是模板参数。我当然可以定义这样的东西

#include <cstdint>
template<int N>
struct sized_uint {};
template<> struct sized_uint<8> { typedef uint8_t type; };
template<> struct sized_uint<16> { typedef uint16_t type; };
template<> struct sized_uint<32> { typedef uint32_t type; };
template<> struct sized_uint<64> { typedef uint64_t type; };

然后在我的模板中使用它,例如一个函数:

template<int N> void myfunc(typename sized_uint<N>::type);

但是在任何版本的 C++ 中是否有像上面定义的标准类型sized_uint

没有这样的标准类型。但是,有 boost::int_t,如果您可以接受 boost 依赖性,它将执行您想要的操作。请注意,语义略有不同,因为 boost::int_t 将为您提供最小的整数类型 至少 那么多位,而不是恰好那么多。