C++ Intermediate · Lesson 01 of 27
auto and type inference
How the compiler deduces types, auto with references and const, and structured bindings.
Estimated time · 45–60 min
Learning goals
What you will understand
- Explain auto and type inference in your own words
- Use it in a small robotics example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
auto deduces a variable's type from its initializer, and the deduced type drops references and top-level const unless you write them: const auto& keeps a read-only reference and avoids a copy. decltype(expr) gives the exact type of an expression.
C++17 adds structured bindings, which unpack a pair, tuple, array or simple struct into named variables. They make loops over maps much easier to read. ROS 2 code uses auto heavily for long types such as shared pointers.
Guided lesson
Work through these steps
- 1.
Read the auto and type inference example and identify what each line owns or changes.
- 2.
Create a small source file, compile with -std=c++17 and warnings enabled, and run it.
- 3.
Change one input, predict the result before running, then compare the output.
- 4.
Record one compiler or runtime error and explain the correction in your own words.
Worked example
Read it before you run it
#include <map>
// Header for std::map.
#include <string>
// Header for std::string.
auto count = 3;
// Deduced as int.
auto gain = 0.75;
// Deduced as double.
const auto& name = std::string{"shoulder"};
// const auto& binds a read-only reference; no copy is made.
std::map<std::string, double> limits{{"shoulder", 1.5}, {"elbow", 2.0}};
// A map from joint name to its limit.
for (const auto& [joint, limit] : limits) {
// Structured binding: unpack each key/value pair into joint and limit.
std::cout << joint << " " << limit << '\n';
// Print each pair.
}
// End of the loop.
decltype(gain) other = 1.5;
// decltype copies the exact type of gain (double).What the example is doing
- 1.Read declarations from right to left: identify each value's type, name, and initial value.
- 2.Follow the data through each expression before considering syntax shortcuts.
- 3.Compile with warnings enabled; a clean build is part of the result, not an optional polish step.
Watch it run · Values in memory
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for auto and type inference. Expected result: elbow 2 shoulder 1.5
Practice code
Now type it yourself
Copy the starter into practice.cpp and replace every TODO with your own code. Build and run it: g++ -std=c++17 -Wall -Wextra -Wpedantic practice.cpp -o practice -pthread && ./practice
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, double> limits{{"shoulder", 1.5}, {"elbow", 2.0}};
// TODO: loop over limits with const auto& and a structured binding [joint, limit]; print "joint limit"
return 0;
}Expected output
elbow 2
shoulder 1.5Stuck? Show the full solution (try it yourself first)
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, double> limits{{"shoulder", 1.5}, {"elbow", 2.0}};
for (const auto& [joint, limit] : limits) {
std::cout << joint << ' ' << limit << '\n';
}
return 0;
}Expected result
How you know it worked
- After adding the shown fragment to the guided exercise with its listed headers and surrounding program, the finished program builds with -Wall -Wextra -Wpedantic.
- Changing the stated input produces the result you predicted.
Practical exercise
Apply the lesson
Rewrite a loop over std::map<std::string, double> with structured bindings and const auto&. Then use decltype to declare a variable with the same type as an existing expression, and explain what auto would have dropped.
Before you continue
- The program builds with -std=c++17 and no warnings
- You can explain the key rule without reading the example
- The normal and edge cases both have recorded results
If something goes wrong
Start at the first error. Check spelling, required #include lines, and whether the declaration appears before use.
Print the inputs and intermediate values, then check conversions, units, and collection order.