Concepts / Type Conversions in Arithmetic

Type Conversions in Arithmetic

Auto lets the compiler deduce a variable's type from its initializer, so the form is auto variable_name = initializer;.

  • Programming

Declaring a Variable Without Naming Its Type

Most variable definitions you have seen so far name the type before the variable, as in double price = 2.5;. The type name tells the reader what kind of value the variable will hold. Auto offers a different route: you leave the type out and let the value on the right-hand side decide it. The equals sign still does its normal job, which is giving the variable its starting value.

What do you think happens?

In the declaration auto x = 123;, what type does x end up with?

  • int

  • double

  • bool

  • char

Reveal answer
Answer:

int

A whole number such as 123 is an int, and auto gives x the type of its initializer, so x is an int. Nothing about the name x or the word auto chooses the type.

Reading the Right-Hand Side First

The rule is simple to apply once you look at the right-hand side first. Whatever kind of value the initializer produces is the kind of value the variable will be. The type of a variable declared with auto is therefore a consequence of the expression you write after the equals sign, not a choice you make on the left.

InitializerKind of valueType auto gives the variable
truea literal truth valuebool
'A'a character in single quoteschar
123a whole numberint
1.2a number with a decimal pointdouble
sqrt(y)the return value of a function callthe return type of sqrt

Each initializer determines the variable's type. The function call row shows that the initializer does not have to be a literal.

cpp
Notes on the code
  • flag takes the type bool from the literal true
  • letter takes the type char from the character literal 'A'
  • count takes the type int from the whole number 123
  • price takes the type double from the decimal literal 1.2

The function-call row deserves a closer look. Suppose a variable z is declared with auto and its initializer is sqrt(y). The variable z does not get a type because of anything you typed next to z. Its type is whatever sqrt(y) returns, and the compiler copies that type onto z. Auto makes this link explicit: the function call is the source of the information. The same idea applies to any expression, so auto is not limited to literal values.

Tracing Where the Type Comes From

When the compiler meets an auto declaration, the path it follows is short. It starts from the initializer expression, determines the kind of value that expression produces, and gives the variable that same type. The variable then holds the value in a variable of exactly that type. The diagram traces this path for a declaration of the form auto variable_name = initializer;.

initializerexpressionthe right-hand side of theequals signkind of valueproducedfor example int or doubletype copied ontovariableno type is named on theleftvalue storedstored in a variable ofthat exact typeevaluatededuceinitialize
What steps does the compiler take from the initializer expression to the final variable type?

Anatomy of the auto Declaration

Each part of an auto declaration has a distinct job. The keyword auto marks the declaration as one whose type will be deduced. The variable_name is the name you choose. The equals sign gives the variable its starting value. The initializer on the right is the part that determines the type.

autokeyword: type is deducedvariable_namethe name you choose=gives the starting valueinitializerits type becomes thevariable's type
Which part of the declaration is the keyword, which is the name, and which is the initializer that determines the type?

Why auto Avoids a Conversion at the Declaration

Conversions are where surprises usually come from. When the type written on the left does not match the type of the value on the right, the value may be changed to fit, and that change may lose information. An explicit type can create this mismatch if you choose a type different from the one the initializer produces.

cpp
Notes on the code
  • The explicit int forces the value 2.5 to be changed to fit an int, which can lose information
  • auto copies the type of 2.5, so no mismatch exists at the declaration
declared type intexplicit type on the leftinitializer 2.5a double value on the rightvalue changed to fitinformation may be lostdeduced type doubletaken from the initializerinitializer 2.5same value on the rightvalue stored as 2.5no conversion at thedeclarationmust matchconvertedmatchesstored
How does the stored value and its type change when a variable is declared with an explicit type versus with auto, and where does the extra conversion disappear?

The source describes the key idea precisely: auto does not change the type of the value. The variable's type is the type of the initializer, so the value is stored in a variable of exactly that type. Because auto copies the type from the value, that mismatch cannot occur at the point of the declaration. This is the sense in which auto avoids a conversion at the start of the variable's life.

Choosing auto or an Explicit Type

The default approach is to use auto when you have no specific reason to mention the type. Auto is the everyday choice, and an explicit type is the exception that needs a justification. The source names two kinds of justification. An explicit type is worth writing when the declaration is in a large scope, or when exact range or precision matters.

Auto also helps with redundancy and long type names. The source notes that this is especially important in generic programming, where the exact type of an object can be hard for the programmer to know and the type names can be quite long. In that setting, auto lets you define the variable without first working out and typing the full name.

SituationPrefer autoPrefer an explicit type
No specific reason to mention the typeYes, this is the defaultNot needed
Long or hard-to-know type names, as in generic codeYes, avoids writing the full nameOnly if the name is needed for clarity
Declaration in a large scopeLess preferredYes, the source names this as a justification
Exact range or precision mattersLess preferredYes, the source names this as a justification

Auto is the everyday choice. An explicit type needs a reason such as scope, range, or precision.

Mistakes With Hidden Types

  • Common mistake

    Assuming auto picks a type from the name of the variable

    What it looks like
    auto total = 2.5;
    Why it fails

    The type comes from the initializer on the right, not from the name total or from what the variable seems to hold.

    Do this instead

    Look at the right-hand side. Here 2.5 has a decimal point, so total takes the type double.

  • Common mistake

    Writing an explicit type that does not match the value and expecting no change

    What it looks like
    int n = 2.5;
    Why it fails

    When the left-hand type does not match the value on the right, the value may be changed to fit and information may be lost.

    Do this instead

    Use auto so the type follows the value, or choose an explicit type that matches the value deliberately.

  • Common mistake

    Using auto everywhere without considering the reader

    Why it fails

    In a large scope or where exact range or precision matters, the type is information the reader needs to see.

    Do this instead

    Keep auto as the default, but write the explicit type when the scope or precision makes it important.

Tracing Declarations by Hand

EASY

For each declaration, state the type the variable receives. Look only at the right-hand side: auto x = true; auto y = 'B'; auto z = 10; auto w = 0.75;

Hints
  • Match each initializer to its kind: true is bool, a character in single quotes is char, a whole number is int, a decimal number is double.
  • Do not look at the variable name to decide the type.
MEDIUM

A colleague writes auto ratio = sqrt(area); in a large function. Explain what determines the type of ratio, and state whether you would keep auto or write an explicit type, giving one reason.

Hints
  • The type of ratio is whatever sqrt(area) returns.
  • Consider the two justifications for an explicit type: large scope, and exact range or precision.

Key Takeaways

  • auto lets the compiler deduce a variable's type from its initializer, using the form auto variable_name = initializer;.
  • The kind of value on the right-hand side determines the type: true gives bool, 'A' gives char, 123 gives int, and 1.2 gives double.
  • Because auto copies the type of the initializer, the declaration itself avoids the conversion that happens when an explicit type does not match the value.
  • Use auto by default, and write an explicit type when the declaration is in a large scope or when exact range or precision matters.

Related concepts

More concepts starting with T