Implicit type conversion

Consider this program:

#include <iostream>

void display(double value)
{
    std::cout << value << '\n';
}

int main()
{
    display(10);  // passing an int to a double parameter

    return 0;
}

The display() function expects a double, but we pass an int. C++ automatically converts the int 10 to double 10.0. This is called implicit type conversion - the compiler performs the conversion automatically.

Key Concept
Type conversion produces a new value - it doesn't modify the original value or variable.

Implicit conversion warnings

Some implicit conversions can lose information:

#include <iostream>

void display(int value)
{
    std::cout << value << '\n';
}

int main()
{
    display(7.8);  // warning: double to int loses fractional part

    return 0;
}

This prints 7 - the fractional part (0.8) is discarded. The compiler warns because data is lost.

Key Concept
Some conversions (like char to int) always preserve the value, while others (like double to int) may lose data. Unsafe implicit conversions typically generate compiler warnings.

Brace initialization prevents these unsafe conversions:

int main()
{
    double temperature{10};  // okay: int to double is safe
    int score{7.8};          // error: double to int is unsafe

    return 0;
}

Explicit type conversion with static_cast

When you intentionally want to convert between types (knowing some data may be lost), use explicit type conversion with static_cast:

static_cast<new_type>(expression)

Example:

#include <iostream>

void display(int value)
{
    std::cout << value << '\n';
}

int main()
{
    display(static_cast<int>(7.8));  // explicitly convert double to int

    return 0;
}

Since we're explicitly requesting the conversion, the compiler won't warn about data loss.

Using static_cast to print char as int

To print a char's numeric value instead of its character:

#include <iostream>

int main()
{
    char letter{'A'};  // ASCII code 65
    std::cout << letter << " has value " << static_cast<int>(letter) << '\n';

    return 0;
}

Output:

A has value 65

The variable letter remains a char - only its value is converted for the output.

Sign conversions

Signed and unsigned integers can be converted using static_cast:

unsigned int positive{12};
int negative{static_cast<int>(positive)};  // value 12 is preserved

When the value fits in both types, it's preserved. When it doesn't fit, wrap-around occurs.

Warning
Converting values that don't fit in the destination type causes wrap-around, which may produce unexpected results.

The std::int8_t character problem

Since std::int8_t is typically an alias for signed char, it may print as a character instead of a number:

#include <cstdint>
#include <iostream>

int main()
{
    std::int8_t score{72};
    std::cout << score << '\n';  // likely prints 'H', not 72!

    return 0;
}

Use static_cast to ensure it prints as an integer:

std::cout << static_cast<int>(score) << '\n';  // always prints 72

This also affects input - std::cin treats std::int8_t as a character, extracting only a single character from the input.