Dashboard/Block 5: Advanced C++/Week 11
Week 11Advanced C++

Templates & Casts

This week we're tackling two things that professional C++ developers reach for all the time: templates and type casts. They sound intimidating, but let's break them down.

Have you ever written a function that finds the max of two ints, then realized you need the exact same thing for doubles, and then again for strings? That's annoying, right? You're copy-pasting the same logic over and over, just changing the type. Templates solve that problem. You write the function oncetemplate<typename T> T getMax(T a, T b) — and the compiler automatically generates the right version for whatever type you throw at it. Think of it like a cookie cutter: you've got one shape (the template), but you can stamp out cookies from any dough (type). And here's the thing — you've been using templates this whole time! vector<int>, vector<string>, vector<double> are all generated from the same class template.

Then there's type casting. In C, you'd just do (int)x and hope for the best. C++ says "nah, let's be more careful about this" and gives you four specific cast operators, each one built for a particular job:

Cast OperatorPurposeSafety LevelExample
static_cast<T>()Safe, well-defined conversions (numeric types, upcasting)Safe (compile-time checked)double d = static_cast<double>(42);
dynamic_cast<T*>()Downcasting polymorphic types (base ptr to derived ptr)Safe (runtime checked, returns nullptr on failure)Circle* c = dynamic_cast<Circle*>(shapePtr);
const_cast<T>()Add or remove const qualifierUse with cautionint* p = const_cast<int*>(constPtr);
reinterpret_cast<T>()Bit-level reinterpretation between unrelated typesDangerous (no checks)Low-level/system programming only

By the end of this week, you'll know how to write generic functions and data structures that work with any type, and you'll know exactly which cast to reach for (and when to just not cast at all).

Learning Objectives

Write function templates for generic algorithms
Create class templates for generic data structures
Understand template specialization basics
Use the four C++ cast operators correctly

Key Concepts

Function and Class Templates

C++ Type Casting