When should I use auto in C++?

The auto keyword seems convenient, but the lesson says to use it sparingly. When is it appropriate to use auto in C++?

The auto keyword in C++ is used for type deduction. When you use auto, the compiler infers the type of the variable from its initializer. This can be convenient and can make your code more readable in certain situations:

Example 1: When the type is long or complicated, especially when using templates:

std::map<std::string,
  std::vector<int>> my_map{GetMap()};
// vs
auto my_map{GetMap()};

Example 2: When the exact type isn't important for understanding the code:

// If we only care that result is,
// say, a number, auto is fine
auto result = some_complex_function();

Example 3: With iterators, where the exact type can be verbose:

for (std::vector<int>::const_iterator it =
  vec.begin(); it != vec.end(); ++it) {
  // ...
}

// vs

for (auto it = vec.begin();
  it != vec.end(); ++it) {
  // ...
}

However, there are also situations where using auto can make your code less readable or even cause bugs:

Example 1: When the type is not immediately clear from the context:

// Is this an int? A long?
// Unclear without more context.
auto x = 5;

Example 2: When you need to be explicit about type conversions:

// The cast is hidden
auto result = static_cast<int>(some_float);

Example 3: When you want to commit to a specific type:

// i is an int
auto i = 5;

// j is a long, which might not be what you want
auto j = 5L;

As a rule of thumb, use auto when it makes your code more readable by avoiding long type names or when the exact type isn't important. But prefer explicit types when the type is not immediately clear, when type conversions are involved, or when you want to commit to a specific type.

Variables, Types and Operators

Learn the fundamentals of C++ programming: declaring variables, using built-in data types, and performing operations with operators

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

How does integer division work in C++?
Can you explain more about how integer division and the modulus operator work in C++, with some examples?
Implicit casts between booleans and numbers in C++
The lesson mentions that numeric types can be implicitly cast to booleans in C++, with 0 being false and anything else true. Can you clarify how that works with an example?
What is uniform initialization in C++?
The lesson recommends using uniform initialization with braces (like int x {5};) instead of the = operator. What advantages does this have?
How does operator precedence work in C++?
Can you clarify the rules around operator precedence in C++? How can I control the order of operations?
Prefix vs postfix increment in C++
What's the difference between the prefix and postfix increment/decrement operators in C++? When should I use one over the other?
Floating point precision in C++
I've heard that floating point numbers can sometimes be imprecise in C++. Can you explain why this happens and how to handle it?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant