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

发布于 2024-08-22 12:11:41 字数 456 浏览 6 评论 0原文

我可以指定 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

少女净妖师 2024-08-29 12:11:41

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
}

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

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
}
计㈡愣 2024-08-29 12:11:41

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 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.

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

热血少△年 2024-08-29 12:11:41

您可以强制转换常数。例如:

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

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

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

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

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.

谈下烟灰 2024-08-29 12:11:41

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

min(9ui8, example2);

不过,您不能使用像“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.

min(9ui8, example2);

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

失去的东西太少 2024-08-29 12:11:41

假设您正在使用 std::min 您实际应该做的是显式指定 min 应该使用什么类型

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

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

unsigned char example2 = 0;
min<unsigned char>(9, example2);
爱格式化 2024-08-29 12:11:41

只需 const unsigned char example = 0; 就可以了。

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

瞄了个咪的 2024-08-29 12:11:41

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

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

谷夏 2024-08-29 12:11:41

无符号字符类型没有后缀。整数常量为 intlongsignedunsigned),在 C99 中为 long long代码>.只要该值在无符号字符的有效范围内,您就可以放心使用普通的“U”后缀。

There is no suffix for unsigned char types. Integer constants are either int or long (signed or 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.

零崎曲识 2024-08-29 12:11:41

问题是如何“在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文