Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Type Systems and Automatic Type Inference Summary
Review and test your understanding of all type system and type inference concepts covered in this chapter.
Type Conversion and auto recap
Excellent work reaching this point! You've learned about C++'s type conversion system, which is fundamental to writing correct and efficient programs. The standard conversion rules are quite complex -- don't worry if you don't grasp every nuance.
Type Conversion Fundamentals
The process of converting a value from one data type to another data type is called a type conversion.
Implicit type conversion (also called automatic type conversion or coercion) is performed whenever one data type is expected but a different data type is supplied. If the compiler can figure out how to do the conversion between the two types, it will. If it doesn't know how, it will fail with a compile error.
Standard Conversions
The C++ language defines numerous built-in conversions between fundamental types (plus a few conversions for more advanced types) called standard conversions. These include numeric promotions, numeric conversions, and arithmetic conversions.
Numeric Promotions
A numeric promotion is the conversion of certain smaller numeric types to certain larger numeric types (typically int or double), so the CPU can operate on data that matches the processor's natural data size. Numeric promotions include both integral promotions and floating-point promotions. Numeric promotions are value-preserving, meaning there's no loss of value or precision. Not all widening conversions are promotions.
Numeric Conversions
A numeric conversion is a type conversion between fundamental types that isn't a numeric promotion. A narrowing conversion is a numeric conversion that may result in loss of value or precision.
Usual Arithmetic Conversions
In C++, certain binary operators require operands of the same type. If operands of different types are provided, one or both operands will be implicitly converted to matching types using rules called the usual arithmetic conversions.
Explicit Type Conversion
Explicit type conversion is performed when the programmer explicitly requests conversion via a cast. A cast represents a request by the programmer to do an explicit type conversion. C++ supports 5 types of casts: C-style casts, static casts, const casts, dynamic casts, and reinterpret casts. Generally you should avoid C-style casts, const casts, and reinterpret casts. static_cast is used to convert a value from one type to a value of another type, and is by far the most used cast in C++.
Type Aliases
Typedefs and type aliases allow the programmer to create an alias for a data type. These aliases are not new types, and act identically to the aliased type. Typedefs and type aliases don't provide any kind of type safety, and care needs to be taken to not assume the alias is different from the type it's aliasing.
Type Deduction with auto
The auto keyword has several uses. First, auto can do type deduction (also called type inference), which will deduce a variable's type from its initializer. Type deduction drops const and references, so be sure to add those back if you want them.
Auto can also be used as a function return type to have the compiler infer the function's return type from the function's return statements, though this should be avoided for normal functions. Auto is used as part of the trailing return syntax.
Key Terminology
- Type conversion: The process of converting a value from one data type to another
- Implicit type conversion: Automatic conversion performed by the compiler
- Automatic type conversion: Another name for implicit type conversion
- Coercion: Another name for implicit type conversion
- Standard conversions: Built-in conversions between fundamental types defined by C++
- Numeric promotion: Conversion of smaller numeric types to larger types (typically int or double)
- Integral promotion: Promotion of integral types to int or larger
- Floating-point promotion: Promotion of float to double
- Value-preserving: Conversion with no loss of value or precision
- Numeric conversion: Type conversion between fundamental types that isn't a promotion
- Narrowing conversion: Numeric conversion that may result in loss of value or precision
- Usual arithmetic conversions: Rules for converting operands to matching types
- Explicit type conversion: Conversion explicitly requested by the programmer via a cast
- Cast: A request by the programmer to do an explicit type conversion
- C-style cast: Legacy cast syntax inherited from C (avoid using)
- static_cast: The most commonly used cast in C++
- const_cast: Cast that removes or adds const (avoid using)
- dynamic_cast: Cast used for polymorphic type conversions
- reinterpret_cast: Cast for low-level bit reinterpretation (avoid using)
- Typedef: Legacy way to create a type alias
- Type alias: An alias for a data type
- auto: Keyword for type deduction
- Type deduction: Deducing a variable's type from its initializer
- Type inference: Another name for type deduction
- Trailing return syntax: Syntax where the return type follows the parameter list
Looking Forward
Type conversion is one of the more complex aspects of C++, but understanding it is essential for writing correct programs. The concepts you've learned—implicit vs explicit conversions, promotions vs narrowing conversions, and type deduction with auto—will help you avoid subtle bugs and write more expressive code. As you continue learning, you'll encounter more advanced conversion scenarios with classes and templates.
Type Systems and Automatic Type Inference Summary - Quiz
Test your understanding of the lesson.
Practice Exercises
Type-Safe Unit Converter
Build a type-safe unit converter that demonstrates understanding of type conversions, auto type deduction, and type aliases. The converter handles temperature, distance, and weight conversions while avoiding common conversion pitfalls.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!