范文健康探索娱乐情感热点
投稿投诉
热点动态
科技财经
情感日志
励志美文
娱乐时尚
游戏搞笑
探索旅游
历史星座
健康养生
美丽育儿
范文作文
教案论文
国学影视

C类型转换规则何时发生隐式类型转换及强制类型转换适用场合

  C++做为强类型语言,要求编译期的类型声明与检查,要求表达式中各操作数的类型(包括赋值操作的左值和右值,以及形参和实参)具有一致性,但同时也允许一定的灵活性,允许类型在一定程度上的兼容,也就是允许类型遵循一定规则下的隐式转换和强制转换。
  The type of the operand(s) determine whether an expression is legal and, if the expression is legal, determines the meaning of the expression. However, in C++ some types are related to one another. When two types are related, we can use an object or value of one type where an operand of the related type is expected. Two types are related if there is a conversion between them.
  表达式是否合法取决于操作数的类型,而且合法的表达式其含义也由其操作数类型决定。但是,在 C++ 中,某些类型之间存在相关的依赖关系。若两种类型相关,则可在需要某种类型的操作数位置上,使用该类型的相关类型对象或值。如果两个类型之间可以相互转换,则称这两个类型相关。1 类型转换规则
  1.1 The Arithmetic Conversions 算术转换
  The language defines a set of conversions among the built-in types. Among these, the most common are the arithmetic conversions, which ensure that the two operands of a binary operator, such as an arithmetic or logical operator, are converted to a common type before the operator is evaluated. That common type is also the result type of the expression.
  C++ 语言为内置类型提供了一组转换规则,其中最常用的是算术转换。算术转换保证在执行操作之前,将二元操作符(如算术或逻辑操作符)的两个操作数转换为同一类型,并使表达式的值也具有相同的类型。
  The rules define a hierarchy of type conversions in which operands are converted to the widest type in the expression. The conversion rules are defined so as to preserve the precision of the values involved in a multi-type expression. For example, if one operand is of type long double, then the other is converted to type long double regardless of what the second type is.
  算术转换规则定义了一个类型转换层次,该层次规定了操作数应按什么次序转换为表达式中最宽的类型。在包含多种类型的表达式中,转换规则要确保计算值的精度。例如,如果一个操作数的类型是 long double,则无论另一个操作数是什么类型,都将被转换为 long double。
  The simplest kinds of conversion are integral promotions. Each of the integral types that are smaller than int char, signed char, unsigned char, short, and unsigned shortis promoted to int if all possible values of that type fit in an int. Otherwise, the value is promoted to unsigned int. When bool values are promoted to int, a false value promotes to zero and true to one.
  最简单的转换为整型提升:对于所有比 int 小的整型,包括 char、signed char、unsigned char、short 和 unsigned short,如果该类型的所有可能的值都能包容在 int 内,它们就会被提升为 int 型,否则,它们将被提升为 unsigned int。如果将 bool 值提升为 int ,则 false 转换为 0,而 true 则转换为 1。
  需要注意的,上述所谓的转换只是运算时的类型转换,存储时还会转换为其声明时的类型。    int a = 6;     double b = 3.7;     int sum;     sum = a + b;   // a会提升为double,a+b的结果会由double降级为int给到sum,但a和sum的存储类型仍然不变
  1.2 Conversions between Signed and Unsigned Types 有符号与无符号类型之间的转换
  When an unsigned value is involved in an expression, the conversion rules are defined to preserve the value of the operands. Conversions involving unsigned operands depend on the relative sizes of the integral types on the machine. Hence, such conversions are inherently machine dependent.
  若表达式中使用了无符号( unsigned )数值,所定义的转换规则需保护操作数的精度。unsigned 操作数的转换依赖于机器中整型的相对大小,因此,这类转换本质上依赖于机器。
  In expressions involving shorts and ints, values of type short are converted to int. Expressions involving unsigned short are converted to int if the int type is large enough to represent all the values of an unsigned short. Otherwise, both operands are converted to unsigned int. For example, if shorts are a half word and ints a word, then any unsigned value will fit inside an int. On such a machine, unsigned shorts are converted to int.
  包含 short 和 int 类型的表达式, short 类型的值转换为 int 。如果 int 型足够表示所有 unsigned short 型的值,则将 unsigned short 转换为 int,否则,将两个操作数均转换为 unsigned int 。例如,如果 short 用半字表示而 int 用一个字表示,则所有 unsigned 值都能包容在 int 内,在这种机器上, unsigned short 转换为 int。
  The same conversion happens among operands of type long and unsigned int. The unsigned int operand is converted to long if type long on the machine is large enough to represent all the values of the unsigned int. Otherwise, both operands are converted to unsigned long.
  long 和 unsigned int 的转换也是一样的。只要机器上的 long 型足够表示 unsigned int 型的所有值,就将 unsigned int 转换为 long 型,否则,将两个操作数均转换为 unsigned long 。
  On a 32-bit machine, long and int are typically represented in a word. On such machines, expressions involving unsigned ints and longs are converted to unsigned long.
  在 32 位的机器上,long 和 int 型通常用一个字长表示,因此当表达式包含 unsigned int 和 long 两种类型,其操作数都应转换为 unsigned long 型。
  Conversions for expressions involving signed and unsigned int can be surprising. In these expressions the signed value is converted to unsigned. For example, if we compare a plain int and an unsigned int, the int is first converted to unsigned. If the int happens to hold a negative value, the result will be converted as described as belows, with all the attendant problems discussed there.
  对于包含 signed 和 unsigned int 型的表达式,其转换可能出乎我们的意料。表达式中的 signed 型数值会被转换为 unsigned 型。例如,比较 int 型和 unsigned int 型的简单变量,系统首先将 int 型数值转换为 unsigned int 型,如果 int 型的值恰好为负数,其结果将以下述方法转换,并带来其所有副作用。The type of an object determines the values that the object can hold. This fact raises the question of what happens when one tries to assign a value outside the allowable range to an object of a given type.
  对象的类型决定对象的取值。这会引起一个疑问:当我们试着把一个超出其取值范围的值赋给一个指定类型的对象时,结果会怎样呢?
  The compiler must adjust the out-of-range value so that it will fit. The compiler does so by taking the remainder of the value modulo the number of distinct values the unsigned target type can hold. An object that is an 8-bit unsigned char, for example, can hold values from 0 through 255 inclusive. If we assign a value outside this range, the compiler actually assigns the remainder of the value modulo 256. For example, we might attempt to assign the value 336 to an 8-bit signed char. If we try to store 336 in our 8-bit unsigned char, the actual value assigned will be 80, because 80 is equal to 336 modulo 256.
  编译器必须调整越界值使其满足要求。编译器会将该值对 unsigned 类型的可能取值数目求模,然后取所得值。比如 8 位的 unsigned char,其取值范围从 0 到 255(包括 255)。如果赋给超出这个范围的值,那么编译器将会取该值对 256 求模后的值。例如,如果试图将 336 存储到 8 位的 unsigned char 中,则实际赋值为 80,因为 80 是 336 对 256 求模后的值。
  (通常,编译器对于signed整型也是同样处理。)2 When Implicit Type Conversions Occur 何时发生隐式类型转换
  The compiler applies conversions for both built-in and class type objects as necessary. Implicit type conversions take place in the following situations:
  编译器在必要时将类型转换规则应用到内置类型和类类型的对象上。在下列情况下,将发生隐式类型转换:
  In expressions with operands of mixed types, the types are converted to a common type:
  在混合类型的表达式中,其操作数被转换为相同的类型:int ival;  double dval; float sum = ival + dval  // ival converted to double
  An expression used as a condition is converted to bool:
  用作条件的表达式被转换为 bool 类型:int ival;  if (ival) // ival converted to bool  while (cin) // cin converted to bool
  Conditions occur as the first operand of the conditional (?:) operator and as the operand(s) to the logical NOT (!), logical AND (&&), and logical OR (||) operators. Conditions also appear in the if, while, for, and do while statements.
  条件操作符(?:)中的第一个操作数以及逻辑非(!)、逻辑与(&&)和逻辑或(||)的操作数都是条件表达式。出现在 if、while、for 和 do while 语句中的同样也是条件表达式。
  An expression used to initialize or assign to a variable is converted to the type of the variable:
  用一表达式初始化某个变量,或将一表达式赋值给某个变量,则该表达式被转换为该变量的类型:int ival = 3.14; // 3.14 converted to int  int *ip;  ip = 0; // the int 0 converted to a null pointer of type int *
  In addition,implicit conversions also occur during function calls.
  另外,在函数调用中也可能发生隐式类型转换。3 C++强制转换类模板
  3.1 static_cast
  Any type conversion that the compiler performs implicitly can be explicitly requested by using a static_cast:
  编译器隐式执行的任何类型转换都可以由 static_cast 显式完成:     double d = 97.0;      // cast specified to indicate that the conversion is intentional      char ch = static_cast(d);
  Such casts are useful when assigning a larger arithmetic type to a smaller type. The cast informs both the reader of the program and the compiler that we are aware of and are not concerned about the potential loss of precision. Compilers often generate a warning for assignments of a larger arithmetic type to a smaller type. When we provide the explicit cast, the warning message is turned off.
  当需要将一个较大的算术类型赋值给较小的类型时,使用强制转换非常有用。此时,强制类型转换告诉程序的读者和编译器:我们知道并且不关心潜在的精度损失。对于从一个较大的算术类型到一个较小类型的赋值,编译器通常会产生警告。当我们显式地提供强制类型转换时,警告信息就会被关闭。
  A static_cast is also useful to perform a conversion that the compiler will not generate automatically. For example, we can use a static_cast to retrieve a pointer value that was stored in a void* pointer:
  如果编译器不提供自动转换,使用 static_cast 来执行类型转换也是很有用的。例如,下面的程序使用 static_cast 找回存放在 void* 指针中的值:     void* p = &d; // ok: address of any data object can be stored in a void*      // ok: converts void* back to the original pointer type      double *dp = static_cast(p);
  When we store a pointer in a void* and then use a static_cast to cast the pointer back to its original type, we are guaranteed that the pointer value is preserved. That is, the result of the cast will be equal to the original address value.
  可通过 static_cast 将存放在 void* 中的指针值强制转换为原来的指针类型,此时我们应确保保持指针值。也就是说,强制转换的结果应与原来的地址值相等。
  3.2 const_cast
  A const_cast, as its name implies, casts away the constness of its expression. For example, we might have a function named string_copy that we are certain reads, but does not write, its single parameter of type char*. If we have access to the code, the best alternative would be to correct it to take a const char*. If that is not possible, we could call string_copy on a const value using a const_cast:
  const_cast ,顾名思义,将转换掉表达式的 const 性质。例如,假设有函数 string_copy,只有唯一的参数,为 char* 类型,我们对该函数只读不写。在访问该函数时,最好的选择是修改它让它接受 const char* 类型的参数。如果不行,可通过 const_cast 用一个 const 值调用 string_copy 函数:     const char *pc_str;      char *pc = string_copy(const_cast(pc_str));
  Only a const_cast can be used to cast away constness. Using any of the other three forms of cast in this case would result in a compile-time error. Similarly, it is a compile-time error to use the const_cast notation to perform any type conversion other than adding or removing const.
  只有使用 const_cast 才能将 const 性质转换掉。在这种情况下,试图使用其他三种形式的强制转换都会导致编译时的错误。类似地,除了添加或删除 const 特性,用 const_cast 符来执行其他任何类型转换,都会引起编译错误。
  3.3 reinterpret_cast
  A reinterpret_cast generally performs a low-level reinterpretation of the bit pattern of its operands.
  reinterpret_cast 通常为操作数的位模式提供较低层次的重新解释。
  A reinterpret_cast is inherently machine-dependent. Safely using reinterpret_cast requires completely understanding the types involved as well as the details of how the compiler implements the cast.
  reinterpret_cast 本质上依赖于机器。为了安全地使用 reinterpret_cast,要求程序员完全理解所涉及的数据类型,以及编译器实现强制类型转换的细节。
  在引入命名的强制类型转换操作符之前,显式强制转换用圆括号将类型括起来实现:     char *pc = (char*) ip;
  The effect of this cast is the same as using the reinterpret_cast notation. However, the visibility of this cast is considerably less, making it even more difficult to track down the rogue cast.
  效果与使用 reinterpret_cast 符号相同,但这种强制转换的可视性比较差,难以跟踪错误的转换。
  Depending on the types involved, an old-style cast has the same behavior as a const_cast, a static_cast, ora reinterpret_cast. When used where a static_cast or a const_cast would be legal, an old-style cast does the same conversion as the respective named cast. If neither is legal, then an old-style cast performs a reinterpret_cast. For example, we might rewrite the casts from the previous section less clearly using old-style notation:
  旧式强制转换依赖于所涉及的数据类型,具有与 const_cast、 static_cast 和 reinterpret_cast 一样的行为。在合法使用 static_cast 或 const_cast 的地方,旧式强制转换提供了与各自对应的命名强制转换一样的功能。如果这两种强制转换均不合法,则旧式强制转换执行 reinterpret_cast 功能。例如,我们可用旧式符号重写上一节的强制转换:     int ival; double dval;      ival += int (dval); // static_cast: converts double to int      const char* pc_str;      string_copy((char*)pc_str); // const_cast: casts away const      int *ip;      char *pc = (char*)ip; // reinterpret_cast: treats int* as char*
  By using a cast, the programmer turns off or dampens normal type-checking. We strongly recommend that programmers avoid casts and believe that most well-formed C++ programs can be written without relying on casts.
  强制类型转换关闭或挂起了正常的类型检查。强烈建议程序员避免使用强制类型转换,不依赖强制类型转换也能写出很好的 C++ 程序。4 相关术语
  arithmetic conversion(算术转换)
  A conversion from one arithmetic type to another. In the context of the binary arithmetic operators, arithmetic conversions usually attempt to preserve precision by converting a smaller type to a larger type (e.g., small integral types, such as char and short, are converted to int).
  算术类型之间的转换。在使用二元算术操作符的地方,算术转换通常将较小的类型转换为较大的类型,以确保精度(例如,将小的整型 char 型和 short 型转换为 int 型)。
  dynamic_cast
  Used in combination with inheritance and run-time type identification.
  用于结合继承和运行时类型识别。
  implicit conversion(隐式类型转换)
  A conversion that is automatically generated by the compiler. Given an expression that needs a particular type but has an operand of a differing type, the compiler will automatically convert the operand to the desired type if an appropriate conversion exists.
  编译器自动实现的类型转换。假设表达式需要某种特定类型的数值,但其操作数却是其他不同的类型,此时如果系统定义了适当的类型转换,编译器会自动根据转换规则将该操作数转换为需要的类型。
  integral promotions(整型提升)
  Subset of the standard conversions that take a smaller integral type to its most closely related larger type. Integral types (e.g. short, char, etc.) are promoted to int or unsigned int.
  整型提升是标准类型转换规则的子集,它将较小的整型转换为最接近的较大数据类型。整型(如 short、char 等)被提升为 int 型或 unsigned int 型。
  reinterpret_cast
  Interprets the contents of the operand as a different type. Inherently machine-dependent and dangerous.
  将操作数内容解释为另一种不同的类型。这类强制转换本质上依赖于机器,而且是非常危险的。
  static_cast
  An explicit request for a type conversion that the compiler would do implicitly. Often used to override an implicit conversion that the compiler would otherwise perform.
  编译器隐式执行的任何类型转换都可以由 static_cast 显式完成。我们常常使用 static_cast 取代由编译器实现的隐式转换。
  -End-

港股11月4日早报10月比亚迪新能源车销量增249万科销售金额同比下降热点聚焦1。11月3日上午,国家发展改革委组织召开全国长协煤流向监管工作视频会议,介绍并部署煤炭中长期合同履约监测系统数据填报工作,委党组成员秘书长赵辰昕同志出席并作讲话。长协煤不电商节此起彼伏,天津旧物网能否借势掘金收割用户?视频加载中双11的包裹还没收到,双12的促销又即将启动,而接下来还有圣诞促销季,年货节面对接踵而来的各种电商节,商家们的促销折扣活动不断刺激许多消费者们冲动消费。加上近几年来,消费财联社午报新能源赛道强势反弹!概念题材活跃两市逾3000股上涨一早盘盘面回顾两市早盘震荡反弹,三大指数高开高走,深成指创业板指均涨超1。汽车整车汽车配件板块拉升,约20股封涨停板。绿色能源相关的风电光伏储能绿电等板块集体走强,吉鑫科技5连板,每日收评两市百股涨停!成长防守投机方向全面开花新能源赛道迎涨停潮财联社11月4日讯,今日三大指数集体反弹,市场情绪活跃,涨停股近百家。风电光伏储能绿电等板块集体走强,风电股掀起涨停潮。此外,新能源车板块保持强势,军工食品白酒钠电池医美等午后拉升明日主题前瞻四季度并网冲刺这种新能源装机总量或成全球最大今日导读四季度并网冲刺装机总量或成全球最大宁德时代已启动产业化布局或与锂电池共享产业发展红利又有公司透露提价信号大消费领域旺季已至已开立个人钱包1。4亿个数字人民币加速落地ABF载汽车零部件个股走强,压制因素逐步缓解,机构四季度有望迎来回暖财联社(上海,编辑梓隆)讯,今日,汽车零部件板块大幅走高,十余只板块个股涨停,截止收盘,上声电子恒帅股份欣锐科技20cm涨停,圣龙股份迪生力香山股份钧达股份台兴股份凯众股份豪能股份券商战略布局北交所中金投行项目储备高度契合北交所定位,中金财富权限开通破7万户,研究领域体系化财联社(北京,记者高云)讯,北交所开市在即,相关基础规则悉数落地,作为资本市场重要参与者的投行,更是加紧抢滩北交所新赛道。作为投资银行中的领头羊,中金公司是如何推进该业务的?财联社风水轮流转,转完大宗转黄金?Taper靴子落地,机构称黄金有望迎来战术性机会,黄金ETF或可布局财联社(北京,记者黎旅嘉)讯,北京时间周四(11月4日)凌晨200,美联储公布利率决议,维持利率在00。25不变,并于11月启动缩债计划,将每月资产购买规模减少150亿美元,基本符带3颗激光雷达,三大靠山,3。5s破百,比亚迪终于遇对手了?新能源车该有的样子大家都清楚,随着新能源汽车时代的到来,依托电池起家的比亚迪成为了众多车企中在电动新能源领域的领头羊,其最新市值高达7599。48亿,基本上已经逼近了吉利和长城这两爆款又稳了,起步200Ps,一公里4毛钱,比CRV大一圈,价格还厚道随着汽车行业的竞争压力越来越大,各大车企为了博得消费者认可,或者是想要在国内竞争激烈的市场环境下站稳脚跟,那也是表现相当努力,而从目前众多爆款车型的特点来看,消费者无外乎关注颜值高皇冠陆放PK四代汉兰达,姊妹车型价格相差7千,到底值不值?提到大7座SUV,大家最先想到的是哪款车呢?对于很多人来说,或许最先想到的就是丰田汉兰达了吧。作为大7座SUV市场的常青树,很多车型推出时或者上市时,或多或少都会参考汉兰达的表现,
心跳源计划罗云熙角色如何从掮客到真正的科研人员剧名乍一听有点悬疑爱情剧的意思,看了剧才知道,原来源计划是剧中的一个科研项目。这部剧讲述的是科研人员守护科研成果的励志故事,以及裘佳宁(宋茜饰)和周小山(罗云熙饰)从相遇相知到彼此美国衰落和当年的英帝国很像!一超优势不再明显,美国还挺得住吗美国挑起的阿富汗战争,最终或将大概率以美国不体面的离去画上句号。在塔利班当局严格的撤离期限内灰溜溜地撤军,似乎已经昭示着美国这个超级大国即使在阿富汗辛勤布局20年,也终究难免被帝国双平台制霸的游戏手柄,莱仕达9607Switch手柄上手体验众所周知,想要在Switch掌机上得到更好的游戏体验,除了自带的红蓝JoyCon手柄之外,还需要配备一款常规的游戏手柄,然而原装手柄动辄五六百的价格,对于普通消费者是比较困难的,那春节乌镇喊你来体验江南水乡,一起过大年乌镇美图秀一秀乌镇水上年市春节将近,很多地方推出过年的玩法,而乌镇,却重现旧时场景,水乡人家枕水而居,以船为运输方式,开启了水上年市的玩法。每逢集市期间,四乡八邻的村居民们摇着船来OPPOFindX3Pro10亿色双主摄120Hz屏很久以前,对于大哥大的需要是打电话,科技的进步让砖式的大哥大瘦身成功,并带上了相相机的功能,而最新发布的OPPOFindX3Pro给用户带来了旗舰级的拍照体验,感谢新浪众测,让我在衢州柯城研学团建自驾好去处2021年4月2日,中国余东乡村未来社区正式开放!衢州柯城余东乡,是中国民间文化艺术之乡,被誉为中国第一农民画村。村里的房屋外墙都是当地农民放下锄头拿起画笔,一笔一线地勾勒的。余东杭州除了西湖,还有这些从东到西,玩转余杭二日游塘栖古镇京杭大运河上仅存的一座七孔石拱桥就在塘栖,距离明代1498年建成至今,已有五百余年,而塘栖也因此闻名,塘栖就像是京杭大运河上的一颗明珠。古镇较小,全程大概一个小时左右即可逛HDMI充电宝?紫米QB816Switch电脑手机投屏智能手机发展到今天,所有的部件都在变化,所有的参数都在上升,唯一艰难前行的,只有一个电量问题。APP的发达,功能的繁多,各种感应器的使用,工作之余再来几局游戏,使原来如NOKIA手杭州钱江新城CBD新晋网红酒店体验在云端看江景盘点2020年中国三大网红酒店,广州雅致酒店一定榜上有名,如今它的缔造者美豪酒店集团旗下品牌雅致酒店,又在杭州钱江新城CBD旁精彩绽放,酒店坐落于杭州钱塘江畔,林立于各标志性商务群以稻为媒以节会友富阳渔山乡打响稻乡渔镇大家看过电影富春山居图没,刘德华演的,那时的华仔还很年轻,虽然电影可能不咋地,但做为富春山居首的富阳渔山乡,可是一个非常优美的小城镇。三面环山,一面临富春江,连空气都变得非常清澈!在开化,大片的油菜花正在开放,周末将有大批游客抵达油菜花什么时候开?哪里的最漂亮?国家东部公园建设,哪里的花儿最漂亮?等我慢慢来告诉你!开化建设国家东部公园,做到了春看花,夏避暑,秋赏叶,冬探梅。那春天看什么花?当然是10多万亩的