c++ - 您如何使用 clang 的新自定义大小 int 功能?

标签 c++ c integer clang extint

最近听说clang有一个新特性,_ExtInt。我知道它可以让您指定整数的大小(奇数甚至像 13 位 int),但是您如何使用它呢?

最佳答案

_ExtInt 将用作普通说明符。例如:

_ExtInt(13) foo;

这里你声明 foo 是 13 位的。记住不要在它前面放 shortlong 类型的关键字(因为它实际上没有意义),尽管你可以放 signedunsigned(signed 是默认值)。 请注意,您不能做类似的事情; _ExtInt(5) + _ExtInt(6)。根据this网站,那是因为:

The WG14 paper proposes integer promotion to the largest of the types (that is, adding an _ExtInt(5) and an _ExtInt(6) would result in an _ExtInt(6)), however the implementation does not permit that and _ExtInt(5) + _ExtInt(6) would result in a compiler error. This was done so that in the event that WG14 changes the design of the paper, we will be able to implement it without breaking existing programs.

这可以通过使用转换来解决:

(_ExtInt(6))AnExtInt5 + AnExtInt6 or static_cast<ExtInt(6)>(AnExtInt5) + AnExtInt6

不仅如此,如果您使用 C++,您还可以做一些非常疯狂的事情:

template<size_t WidthA, size_t WidthB>
  _ExtInt(WidthA + WidthB) lossless_mul(_ExtInt(WidthA) a, _ExtInt(WidthB) b) {
  return static_cast<_ExtInt(WidthA + WidthB)>(a) 
       * static_cast<_ExtInt(WidthA + WidthB)>(b);
} 

here了解更多详情。

额外说明:

  • 添加到 _ExtInt(32)int 将是一个 int。
  • 您的 int 大小可以增加 116,777,215 位。

注意:为了使用此功能,您需要最新版本的 clang,因为更改是在 2020 年 4 月 21 日进行的。

关于c++ - 您如何使用 clang 的新自定义大小 int 功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61411865/

相关文章:

c++ - 在一次处理大量数据时避免Windows中的 "(Not Responding)"标签

c - 斑马拼图 - C 中的约束满足

java - Java中使用hasNextLine,在NextInt之后不会读取NextLine

javascript - JS Number.MAX_SAFE_INTEGER 和 MAX_VALUE 有什么区别?

c++ - C++ 编译如何处理共享库和模板

c++ - Qt信号/插槽问题

c++ - C++中解决对象间的依赖关系

c - sizeof Pointer 因同一架构上的数据类型而异

C - 使用 select() 创建 UTP 服务器/客户端聊天室

haskell - 如何测试 float 是否是haskell中的整数?