如何在 C++ 中指定 unsigned char 类型的整数文字?
我可以指定 unsigned long 类型的整数文字,如下所示:
const unsigned long example = 9UL;
对于 unsigned char 我该如何做同样的事情?
const unsigned char example = 9U?;
这是避免编译器警告所必需的:
unsigned char example2 = 0;
...
min(9U?, example2);
我希望避免我当前拥有的详细解决方法,并且没有“unsigned char”出现在调用 min 的行中,而无需在单独的行上的变量中声明 9:
min(static_cast<unsigned char>(9), example2);
I can specify an integer literal of type unsigned long as follows:
const unsigned long example = 9UL;
How do I do likewise for an unsigned char?
const unsigned char example = 9U?;
This is needed to avoid compiler warning:
unsigned char example2 = 0;
...
min(9U?, example2);
I'm hoping to avoid the verbose workaround I currently have and not have 'unsigned char' appear in the line calling min without declaring 9 in a variable on a separate line:
min(static_cast<unsigned char>(9), example2);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
C++11 引入了用户定义的文字。它可以这样使用:
C++11 introduced user defined literals. It can be used like this:
C 没有提供标准方法来指定宽度小于
int
类型的整数常量。但是,
stdint.h
确实提供了UINT8_C()
宏来执行与您在 C 中获得的功能非常接近的操作。但是大多数人只是使用无后缀(获取
int
常量)或使用U
后缀(获取unsigned int
常量)。它们对于字符大小的值工作得很好,这几乎就是您从 stdint.h 宏中获得的全部内容。C provides no standard way to designate an integer constant with width less that of type
int
.However,
stdint.h
does provide theUINT8_C()
macro to do something that's pretty much as close to what you're looking for as you'll get in C.But most people just use either no suffix (to get an
int
constant) or aU
suffix (to get anunsigned int
constant). They work fine for char-sized values, and that's pretty much all you'll get from thestdint.h
macro anyway.您可以强制转换常数。例如:
您还可以使用构造函数语法:
并非所有编译器都需要 typedef。
You can cast the constant. For example:
You can also use the constructor syntax:
The typedef isn't required on all compilers.
如果您使用的是 Visual C++,并且不需要编译器之间的互操作性,则可以在数字上使用 ui8 后缀,使其成为无符号 8 位常量。
不过,您不能使用像“9”这样的实际字符常量来执行此操作。
If you are using Visual C++ and have no need for interoperability between compilers, you can use the ui8 suffix on a number to make it into an unsigned 8-bit constant.
You can't do this with actual char constants like '9' though.
假设您正在使用
std::min
您实际应该做的是显式指定min
应该使用什么类型Assuming that you are using
std::min
what you actually should do is explicitly specify what typemin
should be using as such只需
const unsigned char example = 0;
就可以了。Simply
const unsigned char example = 0;
will do fine.我认为
'\0'
将是一个值为 0 的字符文字,但我也不明白这一点。I suppose
'\0'
would be a char literal with the value 0, but I don't see the point either.无符号字符类型没有后缀。整数常量为
int
或long
(signed
或unsigned
),在 C99 中为long long
代码>.只要该值在无符号字符的有效范围内,您就可以放心使用普通的“U”后缀。There is no suffix for unsigned char types. Integer constants are either
int
orlong
(signed
orunsigned
) and in C99long long
. You can use the plain 'U' suffix without worry as long as the value is within the valid range of unsigned chars.问题是如何“在 C++ 中指定 unsigned char 类型的整数‘文字’?”。不是如何声明标识符。
您可以在撇号中使用转义反斜杠和八进制数字。 (例如“\177”)
八进制值始终被视为无符号。
The question was how to "specify an integer 'literal' of type unsigned char in C++?". Not how to declare an identifier.
You use the escape backslash and octal digits in apostrophes. (eg. '\177')
The octal value is always taken to be unsigned.