假设我需要创建一个长度为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
?
看看这个http://stackoverflow.com/questions/31334291/select-an-integer-type-based-on-template-integer-parameter/31334843#31334843,和我相当辉煌的答案; =) 。你可以这样做。 – Bathsheba