【发布时间】:2015-07-21 13:50:01
【问题描述】:
假设我需要制作一个长度正好为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 这样的标准类型?
【问题讨论】:
-
看看这个stackoverflow.com/questions/31334291/…,以及我相当出色的答案;=)。你可以类似地处理这个。