如何在 C++ 中指定 unsigned char 类型的整数文字?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2304732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:58:27  来源:igfitidea点击:

How do I specify an integer literal of type unsigned char in C++?

c++integerconstantslanguage-lawyer

提问by WilliamKF

I can specify an integer literal of type unsigned long as follows:

我可以指定一个 unsigned long 类型的整数文字,如下所示:

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 的行中出现“无符号字符”,而无需在单独行的变量中声明 9:

min(static_cast<unsigned char>(9), example2);

采纳答案by WilliamKF

C++11 introduced user defined literals. It can be used like this:

C++11 引入了用户定义的文字。它可以像这样使用:

inline constexpr unsigned char operator "" _uchar( unsigned long long arg ) noexcept
{
    return static_cast< unsigned char >( arg );
}

unsigned char answer()
{
    return 42;
}

int main()
{
    std::cout << std::min( 42, answer() );        // Compile time error!
    std::cout << std::min( 42_uchar, answer() );  // OK
}

回答by Michael Burr

C provides no standard way to designate an integer constant with width less that of type int.

C 没有提供标准方法来指定宽度小于 type 的整数常量int

However, stdint.hdoes provide the UINT8_C()macro to do something that's pretty much as close to what you're looking for as you'll get in C.

但是,stdint.h确实提供了UINT8_C()宏来执行与您在 C 中获得的内容非常接近的内容。

But most people just use either no suffix (to get an intconstant) or a Usuffix (to get an unsigned intconstant). They work fine for char-sized values, and that's pretty much all you'll get from the stdint.hmacro anyway.

但大多数人要么不使用后缀(获得int常数),要么使用U后缀(获得unsigned int常数)。它们适用于字符大小的值,stdint.h无论如何,这几乎是您从宏中获得的全部内容。

回答by janm

You can cast the constant. For example:

您可以投射常量。例如:

min(static_cast<unsigned char>(9), example2);

You can also use the constructor syntax:

您还可以使用构造函数语法:

typedef unsigned char uchar;
min(uchar(9), example2);

The typedef isn't required on all compilers.

并非所有编译器都需要 typedef。

回答by Mr Lister

If you are using Visual C++ and have no need for interoperability between compilers, you can use the ui8suffix on a number to make it into an unsigned 8-bit constant.

如果您使用的是 Visual C++ 并且不需要编译器之间的互操作性,则可以在数字上使用ui8后缀,使其成为无符号的 8 位常量。

min(9ui8, example2);

You can't do this with actual char constants like '9' though.

但是,您不能使用像 '9' 这样的实际字符常量来做到这一点。

回答by E.M.

Assuming that you are using std::minwhat you actually should do is explicitly specify what type minshould be using as such

假设您正在使用std::min您实际应该做的事情是明确指定min应该使用什么类型

unsigned char example2 = 0;
min<unsigned char>(9, example2);

回答by Kyle Lutz

Simply const unsigned char example = 0;will do fine.

只是const unsigned char example = 0;会做的很好。

回答by Axel Gneiting

I suppose '\0'would be a char literal with the value 0, but I don't see the point either.

我想'\0'这将是一个值为 0 的字符文字,但我也不明白这一点。

回答by user261840

There is no suffix for unsigned char types. Integer constants are either intor long(signedor unsigned) and in C99 long long. You can use the plain 'U' suffix without worry as long as the value is within the valid range of unsigned chars.

无符号字符类型没有后缀。整数常量要么是intor long( signedor unsigned) ,要么是C99 long long。只要值在无符号字符的有效范围内,您就可以使用普通的“U”后缀而不必担心。

回答by Clive

The question was how to "specify an integer 'literal' of type unsigned char in C++?". Not how to declare an identifier.

问题是如何“在 C++ 中指定一个 unsigned char 类型的整数‘文字’?”。不是如何声明标识符。

You use the escape backslash and octal digits in apostrophes. (eg. '\177')

您可以在撇号中使用转义反斜杠和八进制数字。(例如。'\177')

The octal value is always taken to be unsigned.

八进制值总是被认为是无符号的。