10.6 — Explicit type conversion (casting) and static_cast

In lesson 10.1 -- Implicit type conversion, we discussed that the compiler can implicitly convert a value from one data type to another through a system called implicit type conversion. When you want to numerically promote a value from one data type to a wider data type, using implicit type conversion is fine.

Many new C++ programmers try something like this:

double d = 10 / 4; // does integer division, initializes d with value 2.0

Because 10 and 4 are both of type int, integer division is performed, and the expression evaluates to int value 2. This value then undergoes numeric conversion to double value 2.0 before being used to initialize variable d. Most likely, this isn’t what was intended.

In the case where you are using literal operands, replacing one or both of the integer literals with double literals will cause floating point division to happen instead:

double d = 10.0 / 4.0; // does floating point division, initializes d with value 2.5

But what if you are using variables instead of literals? Consider this case:

int x { 10 };
int y { 4 };
double d = x / y; // does integer division, initializes d with value 2.0

Because integer division is used here, variable d will end up with the value of 2.0. How do we tell the compiler that we want to use floating point division instead of integer division in this case? Literal suffixes can’t be used with variables. We need some way to convert one (or both) of the variable operands to a floating point type, so that floating point division will be used instead.

Fortunately, C++ comes with a number of different type casting operators (more commonly called casts) that can be used by the programmer to have the compiler perform type conversion. Because casts are explicit requests by the programmer, this form of type conversion is often called an explicit type conversion (as opposed to implicit type conversion, where the compiler performs a type conversion automatically).

Type casting

C++ supports 5 different types of casts: C-style casts, static casts, const casts, dynamic casts, and reinterpret casts. The latter four are sometimes referred to as named casts.

Each cast works the same way. As input, the cast takes an expression whose value will be converted, and a target type. As output, the cast returns a temporary object of the target type that contains the converted value.

The difference between the casts is in what kind of conversions they are allowed to perform.

Key insight

Casts return a temporary object, just like a function that returns by value.

We’ll cover C-style casts and static casts in this lesson.

Related content

We discuss dynamic casts in lesson 25.10 -- Dynamic casting, after we’ve covered other prerequisite topics.

Const casts and reinterpret casts should generally be avoided because they are only useful in rare cases and can be harmful if used incorrectly.

Warning

Avoid const casts and reinterpret casts unless you have a very good reason to use them.

C-style casts

In standard C programming, casts are done via operator(), with the name of the type to convert to placed inside the parentheses, and the value to convert to the right. You may still see these used in code that has been converted from C.

For example:

#include <iostream>

int main()
{
    int x { 10 };
    int y { 4 };

    double d { (double)x / y }; // convert x to a double so we get floating point division
    std::cout << d << '\n'; // prints 2.5

    return 0;
}

In the above program, we use a C-style cast to tell the compiler to convert x to a double. Because the left operand of operator/ now evaluates to a floating point value, the right operand will be converted to a floating point value as well, and the division will be done using floating point division instead of integer division!

C++ will also let you use a C-style cast with a more function-call like syntax:

double d { double(x) / y }; // convert x to a double so we get floating point division

This performs identically to the prior example, but has the benefit of parenthesizing the value being converted (making it easier to tell what is being converted).

Although a C-style cast appears to be a single cast, it can actually perform a variety of different conversions depending on context. This can include a static cast, a const cast or a reinterpret cast (the latter two of which we mentioned above you should avoid). As a result, C-style casts are at risk for being inadvertently misused and not producing the expected behavior, something which is easily avoidable by using the C++ casts instead.

Also, because C-style casts are just a type name, parenthesis, and variable or value, they are both difficult to identify (making your code harder to read) and even more difficult to search for.

Best practice

Avoid using C-style casts.

For advanced readers

A C-style cast tries to perform the following C++ style casts, in order:

  • const_cast
  • static_cast
  • static_cast, followed by const_cast
  • reinterpret_cast
  • reinterpret_cast, followed by const_cast

There is one thing you can do with a C-style cast that you can’t do with C++ casts: C-style casts can convert a derived object to a base class that is inaccessible (e.g. because it was privately inherited).

static_cast should be used to cast most values

By far the most used cast in C++ is the static cast operator, which is accessed via the static_cast keyword. static_cast is used when we want to explicitly convert a value of one type into a value of another type.

You’ve previously seen static_cast used to convert a char into an int so that std::cout prints it as an integer instead of a character:

#include <iostream>

int main()
{
    char c { 'a' };
    std::cout << static_cast<int>(c) << '\n'; // prints 97 rather than a

    return 0;
}

To perform a static cast, we start with the static_cast keyword, and then place the type to convert to inside angled brackets. Then inside parenthesis, we place the expression whose value will be converted. Note how much the syntax looks like a function call to a function named static_cast<type>() with the expression whose value will be converted provided as an argument!

Here’s how we’d use static_cast to solve the problem we introduced in the introduction of this lesson:

#include <iostream>

int main()
{
    int x { 10 };
    int y { 4 };

    // static cast x to a double so we get floating point division
    double d { static_cast<double>(x) / y };  
    std::cout << d << '\n'; // prints 2.5

    return 0;
}

There are two important properties of static_cast.

First, static_cast provides compile-time type checking. If we try to convert a value to a type and the compiler doesn’t know how to perform that conversion, we will get a compilation error.

    // a C-style string literal can't be converted to an int, so the following is an invalid conversion
    int x { static_cast<int>("Hello") }; // invalid: will produce compilation error

Second, static_cast is (intentionally) less powerful than a C-style cast, as it will prevent certain kinds of dangerous conversions (such as those that require reinterpretation or discarding const).

Best practice

Favor static_cast when you need to convert a value from one type to another type.

For advanced readers

When casting to a non-aggregate class type, static cast uses direct initialization, which will consider explicit constructors. We discuss explicit constructors in lesson 14.16 -- Converting constructors and the explicit keyword.

Using static_cast to make narrowing conversions explicit

Compilers will often issue warnings when a potentially unsafe (narrowing) implicit type conversion is performed. For example, consider the following snippet:

int i { 48 };
char ch = i; // implicit narrowing conversion

Casting an int (2 or 4 bytes) to a char (1 byte) is potentially unsafe (as the compiler can’t tell whether the integer value will overflow the range of the char or not), and so the compiler will typically print a warning. If we used list initialization, the compiler would yield an error.

To get around this, we can use a static cast to explicitly convert our integer to a char:

int i { 48 };

// explicit conversion from int to char, so that a char is assigned to variable ch
char ch { static_cast<char>(i) };

When we do this, we’re explicitly telling the compiler that this conversion is intended, and we accept responsibility for the consequences (e.g. overflowing the range of a char if that happens). Since the output of this static cast is of type char, the initialization of variable ch doesn’t generate any type mismatches, and hence no warnings or errors.

Here’s another example where the compiler will typically complain that converting a double to an int may result in loss of data:

int i { 100 };
i = i / 2.5;

To tell the compiler that we explicitly mean to do this:

int i { 100 };
i = static_cast<int>(i / 2.5);

Related content

We discuss more uses of static_cast in relation to class types in lesson 14.13 -- Temporary class objects.

Quiz time

Question #1

What’s the difference between implicit and explicit type conversion?

Show Solution

guest
Your email address will not be displayed
Find a mistake? Leave a comment above!
Correction-related comments will be deleted after processing to help reduce clutter. Thanks for helping to make the site better for everyone!
Avatars from https://gravatar.com/ are connected to your provided email address.
Notify me about replies:  
220 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments