C++ 如何编写无符号短整型文字?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1329163/
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
How to write an unsigned short int literal?
提问by moala
42 as unsigned int is well defined as "42U".
42 作为 unsigned int 被很好地定义为“42U”。
unsigned int foo = 42U; // yeah!
How can I write "23" so that it is clear it is an unsigned short int?
我怎样才能写出“23”,以便很明显它是一个无符号的短整型?
unsigned short bar = 23; // booh! not clear!
EDIT so that the meaning of the question is more clear:
编辑以便问题的含义更清楚:
template <class T>
void doSomething(T) {
std::cout << "unknown type" << std::endl;
}
template<>
void doSomething(unsigned int) {
std::cout << "unsigned int" << std::endl;
}
template<>
void doSomething(unsigned short) {
std::cout << "unsigned short" << std::endl;
}
int main(int argc, char* argv[])
{
doSomething(42U);
doSomething((unsigned short)23); // no other option than a cast?
return EXIT_SUCCESS;
}
采纳答案by Steve Jessop
You can't. Numeric literals cannot have short
or unsigned short
type.
你不能。数字文字不能有short
或unsigned short
类型。
Of course in order to assign to bar
, the value of the literal is implicitly converted to unsigned short
. In your first sample code, you couldmake that conversion explicit with a cast, but I think it's pretty obvious already what conversion will take place. Casting is potentially worse, since with some compilers it will quell any warnings that would be issued if the literal value is outside the range of an unsigned short
. Then again, if you wantto use such a value for a good reason, then quelling the warnings is good.
当然,为了赋值给bar
,文字的值被隐式转换为unsigned short
。在您的第一个示例代码中,您可以使用强制转换使该转换显式,但我认为将发生什么转换已经很明显了。强制转换可能更糟,因为对于某些编译器,如果文字值超出unsigned short
. 再说一次,如果您有充分的理由想使用这样的值,那么消除警告是很好的。
In the example in your edit, where it happens to be a template function rather than an overloaded function, you do have an alternative to a cast: do_something<unsigned short>(23)
. With an overloaded function, you could still avoid a cast with:
在您编辑的示例中,它恰好是一个模板函数而不是一个重载函数,您确实有一个强制转换的替代方法:do_something<unsigned short>(23)
。使用重载函数,您仍然可以避免强制转换:
void (*f)(unsigned short) = &do_something;
f(23);
... but I don't advise it. If nothing else, this only works if the unsigned short
version actually exists, whereas a call with the cast performs the usual overload resolution to find the most compatible version available.
……但我不建议这样做。如果不出意外,这仅在unsigned short
版本实际存在时才有效,而使用强制转换的调用执行通常的重载解析以找到最兼容的可用版本。
回答by AnthonyLambert
unsigned short bar = (unsigned short) 23;
or in new speak....
或者用新的说法....
unsigned short bar = static_cast<unsigned short>(23);
回答by TeaAge81
at least in Visual Studio (at least 2013 and newer) you can write
至少在 Visual Studio(至少 2013 及更新版本)中,您可以编写
23ui16
for get an constant of type unsigned short.
获取 unsigned short 类型的常量。
see definitions of INT8_MIN, INT8_MAX, INT16_MIN, INT16_MAX, etc. macros in stdint.h
参见 stdint.h 中 INT8_MIN、INT8_MAX、INT16_MIN、INT16_MAX 等宏的定义
I don't know at the moment whether this is part of the standard C/C++
我目前不知道这是否是标准 C/C++ 的一部分
回答by Kirill V. Lyadvinsky
There are no modifiers for unsigned short. Integers, which has int
type by default, usually implicitly converted to target type with no problems. But if you really want to explicitly indicate type, you could write the following:
unsigned short 没有修饰符。int
默认情况下具有类型的整数,通常可以毫无问题地隐式转换为目标类型。但是如果你真的想明确指出类型,你可以写如下:
unsigned short bar = static_cast<unsigned short>(23);
As I can see the only reason is to use such indication for proper deducing template type:
正如我所看到的,唯一的原因是使用这样的指示来正确推断模板类型:
func( static_cast<unsigned short>(23) );
But for such case more clear would be call like the following:
但对于这种情况,更清楚的调用如下:
func<unsigned short>( 23 );
回答by MSalters
You probably shouldn't use short, unless you have a whole lot of them. It's intended to use less storage than an int, but that int will have the "natural size" for the architecture. Logically it follows that a short probably doesn't. Similar to bitfields, this means that shorts can be considered a space/time tradeoff. That's usually only worth it if it buys you a whole lot of space. You're unlikely to have very many literals in your application, though, so there was no need foreseen to have short literals. The usecases simply didn't overlap.
你可能不应该使用short,除非你有很多。它旨在使用比 int 更少的存储空间,但该 int 将具有体系结构的“自然大小”。从逻辑上讲,短路可能不会。与位域类似,这意味着可以将短裤视为空间/时间权衡。这通常只有在为您购买大量空间时才值得。但是,您的应用程序中不太可能有很多文字,因此没有必要预见到有短文字。用例根本没有重叠。
回答by Tyler McHenry
Unfortunately, the only method defined for this is
不幸的是,为此定义的唯一方法是
One or two characters in single quotes ('), preceded by the letter L
单引号 (') 中的一或两个字符,以字母 L 开头
According to http://cpp.comsci.us/etymology/literals.html
根据http://cpp.comsci.us/etymology/literals.html
Which means you would have to represent your number as an ASCII escape sequence:
这意味着您必须将您的数字表示为 ASCII 转义序列:
unsigned short bar = L'\x17';
回答by micmoo
Unfortunately, they can't. But if people just look two words behind the number, they should clearly see it is a short... It's not THAT ambiguous. But it would be nice.
不幸的是,他们不能。但是如果人们只看数字后面的两个字,他们应该清楚地看到它是一个简短的......这不是那么模棱两可。但它会很好。
回答by bobbymcr
If you express the quantity as a 4-digit hex number, the unsigned shortness might be clearer.
如果您将数量表示为 4 位十六进制数,则无符号短缩性可能会更清晰。
unsigned short bar = 0x0017;
unsigned short bar = 0x0017;