1. Problem It Solves
C++11 adds notation and metadata for domain values, readable text, encoded strings, compiler-visible intent, memory alignment, and per-thread state. 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 37, plus basic variables, functions, and output already introduced.
3. Core Idea
Mental model: C++11 adds notation and metadata for domain values, readable text, encoded strings, compiler-visible intent, memory alignment, and per-thread state. Identify the relevant value or state, who owns it, and whether the rule acts during compilation or execution.
4. Minimal Syntax
2_kb; R"(raw text)"; u8"text"; [[noreturn]]; alignas(16); thread_local int n;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
A UTF-8 string is still a sequence of code units, so byte indexing is not the same as indexing user-perceived characters.
7. When to Use It
Use it when syntax should express units, text, storage constraints, or thread-local state directly.
Avoid it when it hides ownership, lifetime, type, ordering, or cost.
8. Simple Example
One compact program uses a _kb literal, raw path, UTF-8 text, [[noreturn]], alignas/alignof, and a thread_local counter. The .cpp keeps the data fixed and avoids unrelated abstraction.
Complete sample code
Source file
cpp11/38_literals_raw_strings_unicode_attributes_alignment_thread_local/main.cpp
#include <cstddef>
#include <iostream>
constexpr unsigned long long operator"" _kb(unsigned long long value) {
return value * 1024ULL;
}
thread_local int calls = 0;
struct alignas(16) Block {
char bytes[16];
};
[[noreturn]] void stop() {
throw 1;
}
int main() {
const char* path = R"(C:\ticks\day1)";
const char* text = u8"Giá";
++calls;
std::cout << "bytes=" << 2_kb << '\n';
std::cout << "path=" << path << '\n';
std::cout << "text=" << text << '\n';
std::cout << "alignment=" << alignof(Block) << " calls=" << calls << '\n';
try { stop(); } catch (int) { std::cout << "caught\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 — In C++11, what different needs do
R"(C:\ticks)",alignas(16), andthread_local int callsaddress?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: A UTF-8 string is still a sequence of code units, so byte indexing is not the same as indexing user-perceived characters. What is the smallest C++11-safe correction?