Search code examples
c++typescasting

Is conversion and promotion the same thing?


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?


Solution

  • 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.