C++ 为什么或为什么不应该使用“UL”来指定 unsigned long?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1273687/
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
Why or why not should I use 'UL' to specify unsigned long?
提问by BuckFilledPlatypus
ulong foo = 0;
ulong bar = 0UL;//this seems redundant and unnecessary. but I see it a lot.
I also see this in referencing the first element of arrays a good amount
我也在大量引用数组的第一个元素时看到了这一点
blah = arr[0UL];//this seems silly since I don't expect the compiler to magically
//turn '0' into a signed value
Can someone provide some insight to why I need 'UL' throughout to specify specifically that this is an unsigned long?
有人可以提供一些见解为什么我需要“UL”来具体说明这是一个无符号长整数?
回答by AraK
void f(unsigned int x)
{
//
}
void f(int x)
{
//
}
...
f(3); // f(int x)
f(3u); // f(unsigned int x)
It is just another tool in C++; if you don't need it don't use it!
它只是 C++ 中的另一个工具;如果您不需要它,请不要使用它!
回答by Brian Neal
In the examples you provide it isn't needed. But suffixes are often used in expressions to prevent loss of precision. For example:
在您提供的示例中,不需要它。但是表达式中经常使用后缀来防止精度损失。例如:
unsigned long x = 5UL * ...
You may get a different answer if you left off the UL suffix, say if your system had 16-bit ints and 32-bit longs.
如果您不使用 UL 后缀,您可能会得到不同的答案,比如您的系统是否有 16 位整数和 32 位长整数。
Here is another example inspired by Richard Corden's comments:
这是另一个受 Richard Corden 评论启发的例子:
unsigned long x = 1UL << 17;
Again, you'd get a different answer if you had 16 or 32-bit integers if you left the suffix off.
同样,如果您保留后缀,如果您有 16 位或 32 位整数,您将得到不同的答案。
The same type of problem will apply with 32 vs 64-bit ints and mixing long and long long in expressions.
相同类型的问题将适用于 32 位和 64 位整数以及在表达式中混合 long 和 long long。
回答by Martin York
Some compiler may emit a warning I suppose.
The author could be doing this to make sure the code has no warnings?
我想某些编译器可能会发出警告。
作者可能会这样做以确保代码没有警告?
回答by schanq
Sorry, I realize this is a rather old question, but I use this a lot in c++11 code...
对不起,我意识到这是一个相当老的问题,但我在 c++11 代码中经常使用它......
ul
, d
, f
are all useful for initialising auto
variables to your intended type, e.g.
ul
, d
,f
都可用于将auto
变量初始化为您想要的类型,例如
auto my_u_long = 0ul;
auto my_float = 0f;
auto my_double = 0d;
Checkout the cpp reference on numeric literals: http://www.cplusplus.com/doc/tutorial/constants/
查看关于数字文字的 cpp 参考:http: //www.cplusplus.com/doc/tutorial/constants/
回答by Sam Harwell
You don't normally need it, and any tolerable editor will have enough assistance to keep things straight. However, the places I use it in C# are (and you'll see these in C++):
您通常不需要它,任何可以容忍的编辑器都会有足够的帮助来使事情保持正直。然而,我在 C# 中使用它的地方是(你会在 C++ 中看到这些):
- Calling a generic method (template in C++), where the parameter types are implied and you want to make sure and call the one with an
unsigned long
type. This happens reasonably often, including this one recently:Tuple<ulong, ulong> = Tuple.Create(someUlongVariable, 0UL);
where without theUL
it returnsTuple<ulong, int>
and won't compile. - Implicit variable declarations using the
var
keyword in C# or theauto
keyword coming to C++. This is less common for me because I only usevar
to shorten very long declarations, andulong
is the opposite.
- 调用泛型方法(C++ 中的模板),其中隐含参数类型,并且您希望确保并调用具有
unsigned long
类型的方法。这种情况经常发生,包括最近的这个:Tuple<ulong, ulong> = Tuple.Create(someUlongVariable, 0UL);
没有UL
它返回Tuple<ulong, int>
并且不会编译。 - 使用
var
C# 中的auto
关键字或C++ 中的关键字进行隐式变量声明。这对我来说不太常见,因为我只var
用来缩短很长的声明,ulong
正好相反。
回答by Ran.CohenTawil
When you feel obligated to write down the type of constant (even when not absolutely necessary) you make sure:
当你觉得有义务写下常量的类型时(即使不是绝对必要),你要确保:
- That you always consider how the compiler will translate this constant into bits
- Who ever reads your code will always know how you thought the constant looks like and that you taken it into consideration (even you, when you rescan the code)
- You don't spend time if thoughts whether you need to write the 'U'/'UL' or don't need to write it
- 你总是考虑编译器如何将这个常量转换为位
- 读过你代码的人总会知道你认为常量是什么样子的,并且你已经考虑了它(即使是你,当你重新扫描代码时)
- 你不会花时间思考是否需要写“U”/“UL”或不需要写
also, several software development standards such as MISRA require you to mention the type of constant no matter what (at least write 'U' if unsigned)
此外,一些软件开发标准(例如 MISRA)要求您无论如何都要提及常量的类型(如果未签名,至少写“U”)
in other words it is believed by some as good practice to write the type of constant because at the worst case you just ignore it and at the best you avoid bugs, avoid a chance different compilers will address your code differently and improve code readability
换句话说,一些人认为编写常量类型是一种很好的做法,因为在最坏的情况下,您只需忽略它,在最好的情况下您可以避免错误,避免不同编译器以不同方式处理您的代码并提高代码可读性的机会