1. Problem It Solves
Type aliases shorten meaningful type names, enum class creates scoped enumerators, and nested templates describe containers of containers without lexical ambiguity. It makes an important constraint visible instead of leaving readers to guess. This lesson keeps only the C++11 core that fits one focused day.
2. Prerequisites
The ideas from Day 6, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: Type aliases shorten meaningful type names, enum class creates scoped enumerators, and nested templates describe containers of containers without lexical ambiguity. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
using Scores = std::vector<int>; enum class State { ready, busy };5. How It Works
The example creates a tiny fixed state with no keyboard input.
C++11 or the standard-library contract applies today's rule.
The program prints the important result so it can be checked against the source.
6. Common Mistakes
An
enum classvalue does not implicitly convert toint; forcing old enum assumptions causes a compile error rather than a silent conversion.
7. When to Use It
Use it when a long type needs a domain name or enumerators must not leak into the surrounding scope.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
The program names a score vector, uses a scoped state, and stores a tiny two-dimensional grid as a nested std::vector. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/07_type_aliases_enum_class_nested_templates/main.cpp
#include <iostream>
#include <vector>
using Scores = std::vector<int>;
enum class State { ready, busy };
int main() {
Scores scores{7, 8, 9};
std::vector<std::vector<int> > grid{{1, 2}, {3, 4}};
State state = State::ready;
std::cout << "score=" << scores[1] << '\n';
std::cout << "cell=" << grid[1][0] << '\n';
std::cout << "ready=" << (state == State::ready) << '\n';
}
9. Key Takeaways
The feature is part of the C++11 scope used in this course.
Understand its lifetime, ownership, type, and ordering consequences.
Compile with warnings and prefer the smallest form that makes the rule obvious.
10. Self-Check Questions
Easy — How do
using Scores = std::vector<int>andenum class State { ready, busy }clarify type declarations? Why writeState::ready?Medium — Read the small example described above. What value or state should it print, and which rule produces that result?
Hard — Find and explain the subtle bug in this situation: An
enum classvalue does not implicitly convert toint; forcing old enum assumptions causes a compile error rather than a silent conversion. What is the smallest C++11-safe correction?