【问题标题】:Are there standard integer types with sizes being template parameters?是否有标准整数类型的大小是模板参数?
【发布时间】: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 这样的标准类型?

【问题讨论】:

标签: c++ templates integer


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-30
相关资源
最近更新 更多