Browse by type

Welcome to our Modern C++ Course, specifically tailored for Engineers focusing on Robotics.
std::vector[begin, end)operator[] vs at()reserve()std::arraystd::minmax & std::count_ifstd::erase_ifinline Functions & Variables[[nodiscard]] Attributestd::optional)std::expected<T, E>[[maybe_unused]] AttributeMutex and lock_guardstd::scoped_lockstd::shared_mutexjoin vs detachcondition_variablestd::async & std::futurestd::atomicvariant, any, tuple)
Phase: Foundations
C++ is a high-performance, compiled language created by Bjarne Stroustrup. In robotics, it is the industry standard for real-time systems, providing "zero-cost abstractions" that allow for high-level code with maximum hardware efficiency.
[!NOTE] Design Philosophy: This module is designed to demystify the "black box" of how code reaches hardware. Robotics involves diverse hardware (ARM/x86); if you don't understand the Linker vs. Compiler, you will be paralyzed by the first library error you hit in ROS.
[!NOTE] Modern C++ (C++17): This course focuses specifically on the C++17 Standard, which introduced critical robotics features like
std::optional,std::string_view, and structured bindings.In the world of Robotics, C++ is the undisputed king. It provides zero-cost abstractions—high-level features that compile into highly efficient machine code. When processing point clouds from a LIDAR at 10Hz or calculating inverse kinematics for a robot arm in under a millisecond, performance is critical.
Who Uses This In The Real World?
- Tesla Autopilot — Real-time sensor fusion and vehicle control systems.
- Open Robotics (ROS) — ROS 2 core is written in C++ for real-time distributed systems.
- Boston Dynamics — Control software for Atlas and Spot.
- NVIDIA Isaac — Accelerated robotics simulation and AI pipelines.
Before we write code, it's crucial to understand how your text becomes a robot action.
| Stage | Action | Intermediate Flag |
|---|---|---|
| 1. Preprocessor | Resolves #include and #define (textual copy-paste). |
g++ -E robot.cpp -o robot.i |
| 2. Compiler | Translates C++ to Assembly (CPU-specific logic). | g++ -S robot.cpp -o robot.s |
| 3. Assembler | Converts Assembly to Binary Machine Code (Object Files). | g++ -c robot.cpp -o robot.o |
| 4. Linker | Combines .o files and libraries into a final executable. |
g++ -o robot_init robot.o |
#pragma onceIn robotics frameworks like ROS 2, modular software is broken into header files (.hpp / .h) and source files (.cpp). During compilation, the preprocessor textually pastes the content of every #include directive into the translation unit.
If multiple files include the same header (e.g., IMUSensor.hpp included by both Controller.cpp and Telemetry.cpp), the compiler might attempt to declare the same class or struct multiple times, resulting in redefinition errors.
#pragma once| Feature | Traditional Include Guards | Modern #pragma once |
|---|---|---|
| Syntax | #ifndef HEADER_H / #define HEADER_H / #endif |
#pragma once (single top line) |
| Mechanism | Preprocessor macro tracking | Compiler filesystem tracking |
| Standardization | ISO C++ Standard | Non-standard, but supported by ALL modern compilers (GCC, Clang, MSVC) |
| Error Risk | Macro naming collisions across large projects | Zero naming collision risk |
| Build Speed | Slower (preprocessor parses file to #endif) |
Faster (compiler skips opening file entirely) |
Legacy Guards Example:
#ifndef ROBOTICS_SENSORS_IMU_HPP
#define ROBOTICS_SENSORS_IMU_HPP
struct IMUData {
double ax, ay, az;
double gx, gy, gz;
};
#endif // ROBOTICS_SENSORS_IMU_HPP
Modern Best Practice:
#pragma once
struct IMUData {
double ax, ay, az;
double gx, gy, gz;
};
[!TIP] Industry Rule in ROS 2: Always put
#pragma onceat the very first line of every header file to prevent multi-inclusion build failures and accelerate CMake compile times.
The One Definition Rule (ODR) is a foundational C++ rule that dictates how entity definitions exist across translation units (TUs) during compilation and linking.
.o). Violating this produces a Linker Error (multiple definition of ... or duplicate symbol).inline functions/variables, and template definitions can appear in multiple translation units—provided every definition is token-for-token identical.| Code Placement | Multiple Includes Behavior | Linker Result | Correct Fix |
|---|---|---|---|
| Non-inline function definition in header | Function compiled into multiple .o files |
❌ multiple definition of 'read_sensor()' |
Mark function inline or move definition to .cpp |
| Global variable definition in header | Symbol defined in multiple object files | ❌ multiple definition of 'robot_id' |
Declare as extern int robot_id; in header, define in ONE .cpp |
| Class / Struct definition in header | Included across multiple .cpp files |
✅ OK (guarded by #pragma once) |
Ensure #pragma once is present |
inline function / template in header |
Included across multiple .cpp files |
✅ OK (ODR exempt by standard) | Header-only implementation allowed |
Real-World Robotics ODR Pitfall (Linker Error):
// ❌ BAD: LidarUtils.hpp (Header file)
#pragma once
// Linker error if included by more than one .cpp file!
int global_lidar_count = 0;
void reset_lidar() {
global_lidar_count = 0;
}
Correct Modern C++ Fix:
// ✅ GOOD: LidarUtils.hpp
#pragma once
// inline variables (C++17) and inline functions allow safe multi-TU inclusion!
inline int global_lidar_count = 0;
inline void reset_lidar() {
global_lidar_count = 0;
}
Let's write a simple program that outputs the status of a robot.
Code Example
#include <iostream>
int main() {
std::cout << "[Robot System] Initializing sensors... OK\n";
std::cout << "[Robot System] Ready for C++17 Robotics!\n";
return 0;
}
$ claude mcp add Robotics_CPP_Notes \
-- python -m otcore.mcp_server <graph>