Re:将unsigned long int提升为long int [英] Re: Promoting unsigned long int to long int

查看:130
本文介绍了Re:将unsigned long int提升为long int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pereges< Br ***** @ gmail.comwrites:

[...]

pereges <Br*****@gmail.comwrites:
[...]


#define ulong unsigned long int

#define uchar unsigned char
#define ulong unsigned long int
#define uchar unsigned char



[...]


这些类型已经有了很好的名字。为什么要给他们

新的?


如果你因某些原因必须重命名,请使用typedef,而不是宏。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http ://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

[...]

These types already have perfectly good names already. Why give them
new ones?

If you must rename them for some reason, use typedefs, not macros.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

推荐答案

6月30日晚上8点49分,Keith Thompson< ks ... @ mib.orgwrote:
On Jun 30, 8:49 pm, Keith Thompson <ks...@mib.orgwrote:

这些类型已经有了非常好的名字。为什么要给他们

新的?
These types already have perfectly good names already. Why give them
new ones?



没有目的。只是在我的一些功能中,已经有很多参数了,而且整个东西都不适合单个

行。

No purpose really. Just that in some of my functions, there are too
many parameters already and the whole thing doesn''t fit in single
line.


如果由于某种原因必须重命名它们,请使用typedef,而不是宏。
If you must rename them for some reason, use typedefs, not macros.



ok

ok


pereges< Br ***** @ gmail.comwrites:
pereges <Br*****@gmail.comwrites:

6月30日晚上8:49,Keith Thompson< ks ... @ mib.orgwrote:
On Jun 30, 8:49 pm, Keith Thompson <ks...@mib.orgwrote:

这些类型已经有了非常好的名字。为什么要给他们

新的?
These types already have perfectly good names already. Why give them
new ones?



没有目的。就我的一些功能来说,已经有很多参数了,而且整个东西都不适合单一的

行。


No purpose really. Just that in some of my functions, there are too
many parameters already and the whole thing doesn''t fit in single
line.



所以把它写在多行上。


给定:

#define ulong unsigned long int

#define uchar unsigned char

或者,最好是:

typedef unsigned long int ulong;

typedef unsigned char uchar;


如果我正在阅读您的代码并看到ulong的引用,我就不能理解这意味着什么直到我确认ulong意味着

" unsigned long int" (我可能会把它写成unsigned long)。

我不得不怀疑你是否有一天会改变定义

所以ulong意味着别的东西。


如果你放弃ulong的定义并直接写unsigned long

,我不必怀疑;你的代码会更清晰。


现在,如果你想要一个typedef,其名字说明你是如何使用这个类型的b $ b,而不是它是怎么回事's定义,这是一个不同的

问题。


您使用ulong对于数组索引;使用

size_t更有意义。


你使用uchar对于只能具有值0,1或

的参数2.我会使用int。使用unsigned char可能会节省一些空间,但它的代码大小可能会花费你的代价,因为编译器必须使用
生成代码来扩展1将字节值转换为单词之前它可以对它进行操作,并在存储之前将其缩小到1个字节。

您还会让读者想知道是否存在这个值适合一个字节的原因是一些基本的

(没有)。如果你有这些东西的

数组,那么使用较小的类型是有意义的。

因为它只是一个参数,所以使用int很好。

So write it on multiple lines.

Given:
#define ulong unsigned long int
#define uchar unsigned char
or, preferably:
typedef unsigned long int ulong;
typedef unsigned char uchar;

If I''m reading your code and see a reference to "ulong", I can''t
understand what it means until I''ve confirmed that "ulong" means
"unsigned long int" (which I''d probably write as "unsigned long").
And I have to wonder whether you might some day change the definition
so "ulong" means something else.

If you drop the definition of "ulong" and just write "unsigned long"
directly, I don''t have to wonder; your code will be clearer.

Now if you want a typedef whose name says something about how you''re
using the type, rather than how it''s define, that''s a different
matter.

You use "ulong" for array indices; it would make more sense to use
size_t.

You use "uchar" for a parameter that can only have the value 0, 1, or
2. I''d use int. Using unsigned char might save some space, but it''s
just as likely to cost you in code size, since the compiler has to
generate code to expand the 1-byte value into a word before it can
operate on it, and to shrink it back down to 1 byte before storing it.
You also make the reader wonder whether there''s some fundamental
reason for this value to fit into a byte (there isn''t). If you had an
array of these things, it would make sense to use a smaller type.
Since it''s just a single parameter, using int is fine.


如果由于某种原因必须重命名它们,请使用typedef,而不是宏。
If you must rename them for some reason, use typedefs, not macros.



ok


ok



-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

诺基亚

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


pereges< Br * **** @ gmail.com写道:
pereges <Br*****@gmail.comwrote:

6月30日晚上8:49,Keith Thompson< ks ... @ mib.orgwrote:
On Jun 30, 8:49 pm, Keith Thompson <ks...@mib.orgwrote:


这些类型已经有了非常好的名字。为什么要给他们

新的?
These types already have perfectly good names already. Why give them
new ones?


没有目的。就我的一些功能来说,已经有很多参数了,而且整个东西都不适合单一的

行。
No purpose really. Just that in some of my functions, there are too
many parameters already and the whole thing doesn''t fit in single
line.



你不必把所有论据等都放在一行上,例如


void
quicksort(vector ** parent_vpa,

unsigned long left,

unsigned long right,

unsigned char axis)


会做得很好,甚至可能会增加可读性(并且还有空间用于添加简短评论的空间......)/


问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de

You don''t have to put all arguments etc. on a single line, e.g.

void
quicksort( vector ** parent_vpa,
unsigned long left,
unsigned long right,
unsigned char axis )

will do nicely and may even increases readabilty (and there''s
still space for adding a short comment;-)

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


这篇关于Re:将unsigned long int提升为long int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆