如何确定模板非类型整型常量参数的位数?
我认为 C++ 标准涵盖了这一点,但我还没有找到它。我正在编写一些模板,这些模板将对其非类型整数参数进行算术运算,并且我发现我需要相当于 template < 等模板中的参数“x”的
。MAX_INT
;int x> Foo
理想情况下,有人可以向我指出标准中的段落(如果存在),该段落给出了积分模板参数大小的允许范围以及确定特定实现上的实际位数的任何方法。
--
澄清一下:模板将在类型系统中进行数学计算,作为元编程库的一部分。 “int”永远不会被实例化,也永远不会占用运行时存储。在很多方面,它类似于预处理器完成的数学运算,在这种情况下,我知道整数类型不能保证与“int”的大小相同。我正在寻找的是标准的一部分,该部分说明类型是否相同,如果不同,则在编译期间模板积分参数使用了多少位。
I would assume that this is covered in the C++ Standard, but I've not been able to find it. I am writing some templates that are going to do arithmetic on their non-type integral parameters, and I find I need the equivalent of MAX_INT
for the parameter 'x' in a template like template <int x> Foo
.
Ideally someone could point me to the paragraph in the standard (if one exists) that gives the allowable ranges for the sizes of integral template parameters and any way of determining the actual number of bits on a particular implementation.
--
To clarify: its the templates that will be doing the math within the type system, as part of a metaprogramming library. The 'int' will never be instantiated, and will never take up runtime storage. In many ways its analogous to the math done by the preprocessor, and in that case I know that the integral types are not guaranteed to be the same size as an 'int'. What I am looking for is the part of the standard that says if the types ARE the same or not, and if not, how many bits are used by template integral parameters during compilation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅数字限制。
std::numeric_limits<>::digits
的文档说:Edit:
还有其他方法,如
min()
、max()
等。See numeric limits. The documentation for
std::numeric_limits<>::digits
says:Edit:
There are other methods, such as
min()
,max()
and the like.您需要 numeric_limit 类 http://www.cplusplus.com/reference/std/limits /numeric_limits/
具体来说,
将为您提供 CHAR_MAX、SCHAR_MAX、UCHAR_MAX、SHRT_MAX、USHRT_MAX、INT_MAX、UINT_MAX、LONG_MAX、ULONG_MAX、FLT_MAX、DBL_MAX 或 LDBL_MAX,具体取决于 T。
You need the numeric_limit class http://www.cplusplus.com/reference/std/limits/numeric_limits/
Specifically,
will give you CHAR_MAX, SCHAR_MAX, UCHAR_MAX, SHRT_MAX, USHRT_MAX, INT_MAX, UINT_MAX, LONG_MAX, ULONG_MAX, FLT_MAX, DBL_MAX or LDBL_MAX, depending on T.
请参阅此处: http://www.boost.org/doc/ libs/1_41_0/libs/integer/integer_traits.html
编辑:实际上它没有为您提供有关标准的任何信息,但是您可以获取某些类型的编译时间的最小值和最大值。
Edit2:根据您的更新,我建议您使用 boost .mpl,boost.type_traits< /a> 以及前面提到的 boost.integer_traits 库。
Boost 还提供了一个名为 cstdint.hpp 的标头(boost.integer 库)定义了 boost::uint32_t 或 boost::int32_t 等类型,这将确保您获得支持 32 位的类型。 (当然也适用于 8、16 和 64 位)
使用 boost.type_traits 例如,您可以比较两种类型(如果它们相等)。您将使用 boost: :is_same 。
Boost MPL 为您提供编译时算法、设定条件等,而整数特征将为您提供一种编译时方法来获取类型的限制。
华泰
See here: http://www.boost.org/doc/libs/1_41_0/libs/integer/integer_traits.html
Edit: Actually it's not giving you any information about the standard however you can get the min and max value on compile time for certain types.
Edit2: According to your update I can suggest you to use boost.mpl, boost.type_traitsand the before mentioned boost.integer_traits library.
Boost also provides a header called cstdint.hpp (part of the boost.integer library) which defines types like boost::uint32_t or boost::int32_t which will ensure that you get a type which supports 32 Bits. (Of course also for 8, 16 and 64 Bits)
With boost.type_traits you can for example compare two types if they are equal. You'd be using boost::is_same for it.
Boost MPL offers you compile time algorithms, to make conditions etc and integer traits will offer you a compile time way to get the limits for types.
HTH