简体   繁体   English

C++ 中的固定大小数字

[英]Fixed-size number in C++

So, I'm creating a library that requires fixed-size numbers (not as in uint32_t but custom sizes).所以,我正在创建一个需要固定大小数字的库(不像uint32_t而是自定义大小)。 Therefore, I assumed I could do something similar to this:因此,我认为我可以做类似的事情:

typedef unsigned int custom_size_t:524272; // it's 65535 bytes as max value

However, when I tried this, my IDE (CLion 2020.1) gave me an error: Expected unqualified-id .但是,当我尝试这个时,我的 IDE (CLion 2020.1) 给了我一个错误: Expected unqualified-id Then, I try to compile it.然后,我尝试编译它。 It gives me a bucketload of errors;它给了我一大堆错误; mainly, it gives me the error expected initializer before ':' token .主要是,它expected initializer before ':' token设定项。

I've tried moving the :524272 to the unsigned int part (after), without any expectations of success.我尝试将:524272移动到unsigned int部分(之后),没有任何成功的期望。 Sure enough, I was right.果然,我是对的。 I've already searched for a solution, but they all refer to sizes such as uint32_t and alike.我已经搜索了一个解决方案,但它们都指的是诸如uint32_t之类的大小。

How would I go about this?我将如何 go 关于这个? Oh, and, for reference, this is my code (with a bit obfuscation):哦,作为参考,这是我的代码(有点混淆):

typedef unsigned int custom_size_t_1:524288;
typedef unsigned int custom_size_t_2:524272;

// ... other code ...

No such syntax exists in C++ for integer types of arbitrary sizes.对于任意大小的 integer 类型,C++ 中不存在这样的语法。

Only the sizes of fundamental integer types char , short , int , long and long long are available, which are always powers of two bytes in size.只有基本 integer 类型charshortintlonglong long的大小可用,它们总是大小的两个字节的幂。 The exact width integers are aliases of these types.确切的宽度整数是这些类型的别名。 Implementations may provide other integer types as an extension, but only these are guaranteed to exist by the standard.实现可以提供其他 integer 类型作为扩展,但只有这些类型保证存在标准。

What you can do is define a custom class that behaves like an integer through overloaded operators, but is implemented internally as an array of bytes (array of integers is faster, but then the size must of course be multiple of the integer type).您可以做的是定义一个自定义 class,其行为类似于 integer 通过重载运算符,但在内部实现为字节数组(整数数组更快,但大小当然必须是 Z157DB7DF530023572E518B 类型的倍数)。

This is concept generally called arbitrary precision or multiple precision arithmetic.这是通常称为任意精度或多精度算术的概念。


There is a standard proposal n2472 to add arbitrary width integer types to the language (with different syntax than what you propose).有一个标准提案n2472向语言添加任意宽度 integer 类型(语法与您建议的不同)。 It was recently implemented in Clang as a language extension.最近在 Clang 中作为语言扩展实现。

If you are looking for integers to be used, you are limited to what's available in <cstdint> , which was introduced in C++11如果您正在寻找要使用的整数,则仅限于<cstdint>中可用的内容,这是在 C++11 中引入的

Namely, int8_t, int16_t, etc. https://en.cppreference.com/w/cpp/types/integer即 int8_t、int16_t 等https://en.cppreference.com/w/cpp/types/integer

If you want to manipulate numbers by their bits, you can use bitsets如果你想通过它们的位来操作数字,你可以使用位

If you are looking for are looking to perform math operations with certain precision or larger numbers you can use an external library, such as:如果您正在寻找以特定精度或更大数字执行数学运算,您可以使用外部库,例如:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM