1. Problem It Solves
Brace initialization gives one consistent syntax and rejects narrowing conversions that could silently lose range or precision. 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 7, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: Brace initialization gives one consistent syntax and rejects narrowing conversions that could silently lose range or precision. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
int count{3}; double price{2.5};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
Changing
int x{3.5};from a comment into code is correctly rejected because list initialization forbids that narrowing conversion.
7. When to Use It
Use it when initializing values where accidental data loss must be visible.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
Several scalar values and a built-in array are initialized with braces, then their fixed sum is printed. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/08_uniform_initialization_narrowing/main.cpp
#include <iostream>
int main() {
int count{3};
double price{2.5};
bool active{true};
int values[]{1, 2, 3};
// int bad{3.5}; // error: narrowing conversion
const double total{count * price};
std::cout << "active=" << active << '\n';
std::cout << "total=" << total << '\n';
std::cout << "sum=" << values[0] + values[1] + values[2] << '\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 — Which conversion errors can C++11 brace initialization detect? Why is
int count{3}valid butint count{3.5}invalid?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: Changing
int x{3.5};from a comment into code is correctly rejected because list initialization forbids that narrowing conversion. What is the smallest C++11-safe correction?