什么时候不必要的演员表有用? [英] When are unnecessary casts useful?

查看:68
本文介绍了什么时候不必要的演员表有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在之前的一次讨论中,我收到了一封来自

CB Falconer的说明,表明我需要从长期

转换为int是不必要的,因为编译器会这样做

没有自动演员。


这是真的,但是:


假设


多头b;

char a;


a = b;


在此赋值,long long被截断为一个字节。你这样做原来不需要演员,但是当你完成这个任务时你就丢弃了

数据。


我最近在lcc-win32编译系统中添加了一个警告

当这样的任务发生时,警告你

隐含数据丢弃。


为了避免这个警告,你必须明确地施展LHS以满足RHS的要求,例如:


a =(char )b;


演员表明你的意图清晰。


微软编译器也存在同样的方法。


我已经习惯这样做,或多或少地自动当我向b $ b施放向下时,即从较大型到较小型。


jacob

In one of the previous discussions I received a note from
C.B. Falconer indicating me that a cast from long long
to int was unnecessary since the compiler will do that
without the cast automatically.

This is true, but:

Suppose

long long b;
char a;

a = b;

In this assignment, the long long is truncated to a byte. You do
not need a cast in principle to do this, but you are discarding
data when you do this assignment.

I have recently added a warning to the lcc-win32 compiler system
when an assignment like this happens, to warn you about
the implicit data discarding.

To avoid the warning you have to explicitely cast the LHS to meet
the RHS, like this:

a = (char)b;

The cast makes your intentions clear.

The same approach exists in Microsoft compilers.

I have gotten used to do this, more or less automatically when I
cast "down", i.e. from a bigger to a smaller type.

jacob

推荐答案

jacob navia说:
jacob navia said:

在之前的一个讨论我收到了一封来自

CB Falconer的说明,表明我是演员rom long long

to int是不必要的,因为编译器会自动执行那个

而没有自动转换。


这是真的,但是:


假设


多头b;

char a;


a = b;


在此赋值中,long long被截断为一个字节。
In one of the previous discussions I received a note from
C.B. Falconer indicating me that a cast from long long
to int was unnecessary since the compiler will do that
without the cast automatically.

This is true, but:

Suppose

long long b;
char a;

a = b;

In this assignment, the long long is truncated to a byte.



不一定。在char签名且sizeof(long long)为1的系统上,

不会丢失任何信息。但总的来说,是的,这是正确的。

Not necessarily. On systems where char is signed and sizeof(long long) is 1,
no information will be lost. But in general, yes, that''s correct.


你这样做

原则上不需要演员表来做这个,但是你在执行这项任务时丢弃了

数据。
You do
not need a cast in principle to do this, but you are discarding
data when you do this assignment.



如果你*丢弃数据,那么你试图为char分配一个它无法容纳的价值。在默认情况下char未签名的系统上,

无害。但是,在默认情况下对char进行签名的系统上,

它是溢出的,并且行为未定义,因此警告是一个有用的警告。


在这种情况下演员是否有利似乎是一些争议的问题。就个人而言,我认为使用强制转换抑制

编译器的诊断消息是不明智的,但是 - 没有达到/ share /

相反的观点 - 我至少可以看到其中的优点,例如

这个。


< snip>

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。

If you *are* discarding data, then you''re attempting to assign to the char a
value that it cannot hold. On systems where char is unsigned by default,
that''s not harmful. On systems where char is signed by default, however,
it''s an overflow, and the behaviour is undefined, so the warning is a
useful one.

Whether a cast is beneficial in this situation seems to be a matter of some
dispute. Personally, I think it''s unwise to use a cast to suppress a
compiler''s diagnostic messages, but - without going so far as to /share/
the opposing view - I can at least see the merit therein, in cases such as
this one.

<snip>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


2007年1月9日星期二14:16:29 + 0100,jacob navia

< ja *** @ jacob.remcomp.frwrote in comp.lang.c:
On Tue, 09 Jan 2007 14:16:29 +0100, jacob navia
<ja***@jacob.remcomp.frwrote in comp.lang.c:

其中一个之前的讨论我收到了来自

CB Falconer的说明,表明我需要从长期

转换为int是不必要的,因为编译器会这样做

没有自动演员。


这是真的,但是:


假设


很长的b;

char a;


a = b;


在此作业中,漫长的是截断为一个字节。你这样做原来不需要演员,但是当你完成这个任务时你就丢弃了

数据。


我最近在lcc-win32编译系统中添加了一个警告

当这样的任务发生时,警告你

隐含数据丢弃。


为了避免这个警告,你必须明确地施展LHS以满足RHS的要求,例如:


a =(char )b;


演员表明你的意图清晰。


微软编译器也存在同样的方法。


我已经习惯这样做,或多或少地自动当我向b $ b施放向下时,即从较大型到较小型。
In one of the previous discussions I received a note from
C.B. Falconer indicating me that a cast from long long
to int was unnecessary since the compiler will do that
without the cast automatically.

This is true, but:

Suppose

long long b;
char a;

a = b;

In this assignment, the long long is truncated to a byte. You do
not need a cast in principle to do this, but you are discarding
data when you do this assignment.

I have recently added a warning to the lcc-win32 compiler system
when an assignment like this happens, to warn you about
the implicit data discarding.

To avoid the warning you have to explicitely cast the LHS to meet
the RHS, like this:

a = (char)b;

The cast makes your intentions clear.

The same approach exists in Microsoft compilers.

I have gotten used to do this, more or less automatically when I
cast "down", i.e. from a bigger to a smaller type.



我完全没问题,只要有办法

禁用警告。

有时候有充分的理由去做不便携的东西。

例如,我的大多数编程都是嵌入式系统中的

实时性能要求,内存限制问题,

或两者。


编译器可以,也许应该使用

默认值,而不是大多数情况下的低警告级别。但是应该有一种方法可以禁用基于运行时值的实施定义或未定义行为的法律代码诊断

时间。


这不仅仅是个人偏好,也是因为很多虚假警告会增加噪音水平。并且可能导致

严重诊断被忽略。


-

Jack Klein

http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a ... FAQ-acllc.html

I have absolutely no problem with this as long as there is a way to
DISABLE the warning.

There are times when there is a good reason to do non-portable things.
Most of my programming, for example, is in embedded systems with
either real time performance requirements, memory limitation issues,
or both.

A compiler could, and perhaps should, use a high warning level by
default, instead of a low warning level as most do. But there should
be a way to disable diagnostics for legal code that might have
implementation-defined or undefined behavior based on values at run
time.

This is not just a personal preference, it is about the fact that too
many spurious warnings increase the "noise level" and can cause
serious diagnostics to be overlooked.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


2007年1月9日星期二13:28:31 +0000,Richard Heathfield

< rj *@see.sig.invalidwrote在comp.lang.c中:
On Tue, 09 Jan 2007 13:28:31 +0000, Richard Heathfield
<rj*@see.sig.invalidwrote in comp.lang.c:

jacob navia说:
jacob navia said:

在之前的一次讨论中,我收到了来自

的注释CB Falconer表示我不需要长期使用
到int的演员阵容,因为编译器会这样做

没有演员表自动。


这是真的,但是:


假设


多头b; < br $> b $ b char a;


a = b;


在此赋值中,long long被截断为一个字节。
In one of the previous discussions I received a note from
C.B. Falconer indicating me that a cast from long long
to int was unnecessary since the compiler will do that
without the cast automatically.

This is true, but:

Suppose

long long b;
char a;

a = b;

In this assignment, the long long is truncated to a byte.



不一定。在char签名且sizeof(long long)为1的系统上,

不会丢失任何信息。但总的来说,是的,这是正确的。


Not necessarily. On systems where char is signed and sizeof(long long) is 1,
no information will be lost. But in general, yes, that''s correct.


你这样做

原则上不需要演员表来做这个,但是你在执行这项任务时丢弃了

数据。
You do
not need a cast in principle to do this, but you are discarding
data when you do this assignment.



如果你*丢弃数据,那么你试图为char分配一个它无法容纳的价值。在默认情况下char未签名的系统上,

无害。但是,在默认情况下对char进行签名的系统上,

它是溢出的,并且行为未定义,因此警告是一个有用的警告。


If you *are* discarding data, then you''re attempting to assign to the char a
value that it cannot hold. On systems where char is unsigned by default,
that''s not harmful. On systems where char is signed by default, however,
it''s an overflow, and the behaviour is undefined, so the warning is a
useful one.



当然,你的意思是,结果是实现定义的(或者在一些不存在的平台上),引发了一个实现定义的信号。

Of course you mean, the result is implementation-defined or (on some
nonexistent platforms) an implementation-defined signal is raised.


在这种情况下演员是否有益似乎是一些争议的问题。就个人而言,我认为使用强制转换抑制

编译器的诊断消息是不明智的,但是 - 没有达到/ share /

相反的观点 - 我至少可以看到其中的优点,例如

这个。


< snip>
Whether a cast is beneficial in this situation seems to be a matter of some
dispute. Personally, I think it''s unwise to use a cast to suppress a
compiler''s diagnostic messages, but - without going so far as to /share/
the opposing view - I can at least see the merit therein, in cases such as
this one.

<snip>



-

Jack Klein

主页: http://JK-Technology.Com



comp.lang.c的常见问题解答 http://c-faq.com/

comp .lang.c ++ http://www.parashift.com/c++-faq -lite /

alt.comp.lang.learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


这篇关于什么时候不必要的演员表有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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