是否有标准整数类型,其大小为模板参数? [英] Are there standard integer types with sizes being template parameters?

查看:131
本文介绍了是否有标准整数类型,其大小为模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Suppose I need to make a template with a member of exactly N bits in length, where N is the template parameter. I could of course define something like this

#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; };

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

and then use it in my template for e.g. a function:

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

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

But are there any standard types like the above defined sized_uint in any version of C++?

推荐答案

没有类似的标准类型。但是,有 boost :: int_t ,如果你想要的话,它会做你想要的你可以接受提升依赖性。请注意,语义略有不同,因为 boost :: int_t 将为您提供最小的整数类型,至少那么多位,而不是正好那么多。

There is no standard type like that. However, there is boost::int_t, which will do what you want if the boost dependency is acceptable for you. Note that the semantics are slightly different, in that boost::int_t will give you the smallest integral type with at least that many bits, rather than exactly that many.

这篇关于是否有标准整数类型,其大小为模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆