Is there's a difference between promotion and widening, I've heard that widening only describes integral promotion.
-
Formally there's no concept of "widening", types can be either converted or promoted (which is still just a kind of conversion). – user657267 Jan 28 '15 at 06:09
-
@user657267 fyi, the C99 rationale does use the term "wider" [when talkin about the usual arithmetic conversions and thus is taking about promotions as well](http://stackoverflow.com/a/24372323/1708801) – Shafik Yaghmour Jan 28 '15 at 10:14
-
@ShafikYaghmour the question is tagged c++, and I can't find a single reference to widening in the current standard. – user657267 Jan 28 '15 at 13:53
-
@user657267 C++ inherits this from C and the rationale document I link to in my answer while not normative is usually taken at face value for language-lawyer questions since it was written to explain certain decisions were made. – Shafik Yaghmour Jan 28 '15 at 13:58
-
@ShafikYaghmour Anything c++ inherits from c is explicitly mentioned in the c++ standard itself. I agree that it's pretty obvious what "wideneing" implies here only that it isn't a formal c++ concept, unlike c. – user657267 Jan 28 '15 at 21:35
2 Answers
Widening "typically" refers to integral/floating point types (as in a char
going to a long
or float
to double
), but it can also refer to character widening (as in going from a char
type to a wchar_t
type).
Widening conversions are also known as "promotions" and narrowing conversions are known as "coercion".
The notion of "promotion" and "coercion" can also be used in the OO since as well (polymorphism); as in promotion of a base class to a derived type, or coercion of derived type to base. In this since it's still a "widening" and "narrowing" as the address space used for the base is "less" than the derived type (hence you are widening/promoting your types when "up-casting", or narrowing/coercing your types when "down-casting").
So to answer directly: Is there's a difference between promotion and widening
.. no not really (unless you are feeling pedantic), though I probably wouldn't say "widen that class type" over "promote that class type" if I was talking about non-integrals (just to avoid any possible initial confusion).

- 6,625
- 1
- 30
- 39
-
2
-
@rony_t, yes, `floating point integral` and `double integral` (though speaking in terms of CS, they are not of type `int`) – txtechhelp Jan 28 '15 at 07:35
-
I think that this term is confusing (even if technically correct), I think that the terms integral types and floating point types are more appropriate. – Jan 28 '15 at 07:42
-
-
Floating point types in C++ are definitely not integral types. Integral types are only the signed and unsigned types whose possible values are whole numbers (with `bool` being an edge case, included for consistency). Even in CS I have never heard a definition which included floating-point types as a subset of integral types. FP and integral types in CS are mutually exclusive subsets of numerical types. _Fixed_ point and integral types, now there you have some ambiguity: an integral type is theoretically a fixed-point number where the fractional part has zero length. – MSalters Jan 28 '15 at 10:08
It really depends on context, because the term "widening" is an informal term, and the meaning varies a bit depending on who is telling the story. I'll describe some common interpretations (but not the only ones).
Before doing that, it is necessary to describe what promotions are. The C++ standard describes integral promotions (between integral types) and floating point promotions (between floating point types). Conversion between an integral type and a floating point type is not described as a promotion.
The common features are that promotions are generally value preserving (except from signed to unsigned integral types, which uses modulo arithmetic) but need not involve increasing the size of a variable (or range of values it can represent). For example, a short may be promoted to an int, but a short and an int may also be the same size (albeit that is implementation/compiler dependent).
The C++ standard doesn't use the term "widening" at all (except in some contexts in the library, unrelated to type conversions). A common informal meaning, in context of integral and floating point conversions, is a promotion that is BOTH value preserving AND to a larger type. The implementation is typically setting the additional bits in the result to zero (i.e. making the value wider without fiddling the bits that represent it). So signed char to short, short to long, unsigned char to unsigned short are widening conversions (assuming none of the types are equal size). Similarly, float to double is a widening conversion (the standard guarantees that the values a float can represent is a strict subset of the values that a double can represent). Conversion from int to double is not a widening (e.g. not necessarily value preserving, bits may be fiddled).
Widening is also sometimes used to describe a conversion of a pointer to derived class into a pointer to base class (or between similar references). The reverse is called "narrowing" and - in C++ - can only be forced with an explicit type conversion.

- 1,966
- 9
- 13