符号/无符号..? [英] signed/unsigned..?
问题描述
在大型c ++设计中,John Lakos建议你应该避免使用
unsigned。关键字并使用常规的整数。现在我知道这本书被认为已经过时了,而且我确实使用了命名空间(他还建议避免使用
)和其他东西,但是这个特别的主题令我感到困惑。
问题是stl有很多size_t类型的未签名和
因为我运行了无警告策略,这迫使我做了一个如果
使用纯int'进行大量的转换。另一方面,当你开始有未签名的
变量时,我发现你可能会遇到非常微妙的错误
并且难以追踪。
任何建议和经验表示赞赏。
S?ren
" S?ren Johansen" < NOSPAM @请>在消息中写道
news:3f ********************* @ dread11.news.tele.dk。 ..
在大型c ++设计中,John Lakos建议你应该避免使用
unsigned。关键字并使用常规的整数。现在我知道这本书被认为已经过时了,我确实使用名称空间(他也建议避免)和其他东西,但这个特别的主题令我感到困惑。
问题是stl有很多size_t类型的未签名和
因为我运行了一个无警告策略,这迫使我在使用纯int'时进行大量的转换。另一方面,当您开始使用未签名的变量时,我发现您可能会遇到非常微妙且难以追踪的错误。
>
我不知道Lakos在什么情况下发表了声明。
但假设size_t是unsigned int或其他任何东西都是不正确的。
以std :: string为例。
#include< string>
int main()
{
std :: string Buffer(" abc");
std :: string :: size_type loop = Buffer.length();
}
如果你把Buffer.length()放到unsigned int中,结果可能/可能不是
是否正确取决于是否
std :: string :: size_type实际上是unsigned int或不是。所以为了安全起见
使用班级提供的尺寸类型。
HTH,
J.Schafer
..
S?ren Johansen写道:
在大型c ++设计中,John Lakos建议你应该避免使用
unsigned。关键字并使用常规的整数。
我不同意这一点。特别是对于范围检查,最好使用
无符号值。请看下面的例子。
int i = ...
if(i> 0 || i< 1000)doWhateverYouLike
使用unsigned int这变成
unsigned int = ...
if(i< 1000)doWhateverYouLike
-
Dipl.-通知。 Hendrik Belitz
电子中心实验室
研究中心Juelich
Hendrik Belitz写道:
我不同意这一点。特别是对于范围检查,最好使用无符号值。请看下面的例子。
int i = ...
if if(i> 0 || i< 1000)doWhateverYouLike
使用unsigned int这变成了
unsigned int = ...
if if(i< 1000)doWhateverYouLike
int i = ...
断言(i> = 0);
同样适用,如果违反约束则会导致错误。
>
Hi,
in Large Scale c++ Design, John Lakos suggests that you should avoid the
"unsigned" keyword and use regular ints. Now I know that this book is
considered mostly outdated and I do use namespaces (which he also suggests
avoiding) and other things, but this particular topic has me puzzled.
The problem is that stl has a lot of size_t types that are unsigned and
since I run a no-warning policy, this forces me to do a lot of casting if
using pure int''s. On the other hand, when you start to have unsigned
variables, I find that you risk getting errors that can be extremely subtle
and hard to track down.
Any advice and experience appreciated.
S?ren
"S?ren Johansen" <nospam@please> wrote in message
news:3f*********************@dread11.news.tele.dk. ..Hi,
in Large Scale c++ Design, John Lakos suggests that you should avoid the
"unsigned" keyword and use regular ints. Now I know that this book is
considered mostly outdated and I do use namespaces (which he also suggests
avoiding) and other things, but this particular topic has me puzzled.
The problem is that stl has a lot of size_t types that are unsigned and
since I run a no-warning policy, this forces me to do a lot of casting if
using pure int''s. On the other hand, when you start to have unsigned
variables, I find that you risk getting errors that can be extremely subtle
and hard to track down.
I don''t know in what context Lakos has made the statement.
But it''s not correct to assume size_t is unsigned int or anything else.
Take example of std::string.
#include <string>
int main()
{
std::string Buffer("abc");
std::string::size_type loop = Buffer.length ();
}
If you take Buffer.length() to fit into an unsigned int, the result may/may not
be correct depending on whether
std::string::size_type is actually unsigned int or not. So to be safe always
use the size types provided by the class.
HTH,
J.Schafer
..
S?ren Johansen wrote:
Hi,
in Large Scale c++ Design, John Lakos suggests that you should avoid the
"unsigned" keyword and use regular ints.
I don''t agree with this. Especially for range checking purposes the usage of
unsigned values is preferable. Just look at the following example.
int i = ...
if ( i > 0 || i < 1000 ) doWhateverYouLike
using unsigned int this becomes
unsigned int = ...
if ( i < 1000 ) doWhateverYouLike
--
Dipl.-Inform. Hendrik Belitz
Central Laboratory of Electronics
Research Center Juelich
Hendrik Belitz wrote:
I don''t agree with this. Especially for range checking purposes the usage of
unsigned values is preferable. Just look at the following example.
int i = ...
if ( i > 0 || i < 1000 ) doWhateverYouLike
using unsigned int this becomes
unsigned int = ...
if ( i < 1000 ) doWhateverYouLike
int i = ...
assert(i >= 0);
works just as well and causes an error if the constraint is violated.
这篇关于符号/无符号..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!