Is conversion and promotion the same thing?

蹲街弑〆低调 提交于 2021-02-07 02:01:16

问题


I am not sure if promotion just means converting a data type to a larger data type (for example short to int).

Or does promotion means converting a data type to another "compatible" data type, for example converting a short to an int, which will keep the same bit pattern (the extra space will be filled with zeros). And is conversion means converting something like an int to a float, which will create a completely different bit pattern?


回答1:


There are two things that are called promotions: integral promotions and floating point promotions. Integral promotion refers to integral types (including bitfields and enums) being converted to "larger" integral types and floating point promotion is specifically just float to double.

Both types of promotions are subsets of a wider range of conversions.

  • char -> int: integral promotion
  • float -> double: floating point promotion
  • int -> char: [narrowing] conversion (not a promotion)
  • int -> float: conversion
  • const char* -> std::string: conversion
  • Foo -> Bar: possibly undefined conversion?
  • etc.



回答2:


A promotion is a specific kind of conversion for built-in types that is guaranteed not to change the value.

The type you are promoting to must be able to accurately represent any possible value of the type you are promoting from.

Here is a list of the applicable conversions.



来源:https://stackoverflow.com/questions/28184597/is-conversion-and-promotion-the-same-thing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!