Advanced · Module 9 · Lesson 2 of 7
Class Templates: A Generic Pair and Container
Turn the Pair sketch from the example into a full class template, separate interface from implementation, and predict per-type instantiation.
By the end you can
- Write a class template with correct member definitions outside the class
- Explain how Pair avoids code duplication across type pairs
- Predict instantiation, size and copy behavior for chosen arguments
Builds on Function Templates: One max for Every Type.
01 · A question first
You need a pair of int and string for shelf coordinates, then a pair of string and double for pricing, then a tiny fixed buffer for each. Copying the class three times drifts within a week. How does one template stamp out each variant safely, and where does the compiler store the generated code?
Make a guess before you read on.
02 · The idea
Members only
Unlock the full lesson
This lesson is part of the membership. Join to read it in full, along with every other premium deep dive.
What's inside
- Start with the problem
- How the idea works
- A second runnable example
- Why it is correct
- What it costs
- Pitfalls and variations
- Where it is used
03 · Practice
Asked in interviews
Something wrong with this lesson? Report a problem