1# C++ Examples
2
3This directory contains comprehensive C++ example programs demonstrating modern C++ features and best practices.
4
5## Files
6
7| File | Description | Key Topics |
8|------|-------------|------------|
9| `01_modern_cpp.cpp` | Modern C++ features (C++17/C++20) | Structured bindings, `std::optional`, `std::variant`, `std::any`, `if constexpr`, fold expressions, `std::filesystem`, concepts |
10| `02_stl_containers.cpp` | STL containers and algorithms | `vector`, `map`, `unordered_map`, `set`, algorithms, ranges (C++20), lambdas |
11| `03_smart_pointers.cpp` | Smart pointer patterns | `unique_ptr`, `shared_ptr`, `weak_ptr`, custom deleters, polymorphism |
12| `04_threading.cpp` | Multithreading and concurrency | `std::thread`, mutex, `std::async`, `std::future`, condition variables, thread-safe queue, `std::jthread` (C++20) |
13| `05_design_patterns.cpp` | Common design patterns | Singleton, Observer, Strategy, RAII, CRTP, Factory |
14| `06_templates.cpp` | Template metaprogramming | Function/class templates, specialization, variadic templates, SFINAE, concepts, type traits |
15| `07_move_semantics.cpp` | Move semantics and value categories | lvalue/rvalue, move constructor/assignment, `std::move`, `std::forward`, perfect forwarding, Rule of Five/Zero |
16
17## Building
18
19### Build all examples
20```bash
21make all
22```
23
24### Build specific example
25```bash
26make modern # Build 01_modern_cpp
27make stl # Build 02_stl_containers
28make smart # Build 03_smart_pointers
29make threading # Build 04_threading
30make patterns # Build 05_design_patterns
31make templates # Build 06_templates
32make move # Build 07_move_semantics
33```
34
35### Run specific example
36```bash
37make run-01_modern_cpp
38make run-02_stl_containers
39# etc.
40```
41
42### Clean build artifacts
43```bash
44make clean
45```
46
47### Display help
48```bash
49make help
50```
51
52## Requirements
53
54- **Compiler**: g++ or clang++ with C++20 support
55- **Standard**: C++20 (minimum C++17 for some examples)
56- **Flags**: `-std=c++20 -Wall -Wextra -O2 -pthread`
57
58### Checking compiler version
59```bash
60g++ --version
61```
62
63Make sure your compiler supports C++20:
64- GCC 10+ or Clang 10+
65
66## Running Examples
67
68Each example is a standalone executable:
69
70```bash
71./01_modern_cpp
72./02_stl_containers
73./03_smart_pointers
74./04_threading
75./05_design_patterns
76./06_templates
77./07_move_semantics
78```
79
80## Example Output
81
82### 01_modern_cpp
83```
84Modern C++ Features Demo
85========================
86
87=== Structured Bindings (C++17) ===
88Tuple: 42, hello, 3.14
89Alice: 95
90Bob: 87
91...
92```
93
94### 02_stl_containers
95```
96STL Containers and Algorithms Demo
97===================================
98
99=== std::vector ===
100Original: 5 2 8 1 9
101After push_back(42): size=6
102Sorted: 1 2 5 8 9 42
103...
104```
105
106### 03_smart_pointers
107```
108Smart Pointers Demo
109===================
110
111=== unique_ptr (Exclusive Ownership) ===
112 [Resource 'unique-1' created]
113 Using resource 'unique-1', data=42
114Moving ownership...
115...
116```
117
118## Key Features Demonstrated
119
120### Modern C++ (C++11/14/17/20)
121- Structured bindings (C++17)
122- `std::optional`, `std::variant`, `std::any`
123- `if constexpr` (C++17)
124- Fold expressions (C++17)
125- Concepts (C++20)
126- Ranges (C++20)
127- `std::jthread` (C++20)
128
129### Memory Management
130- RAII (Resource Acquisition Is Initialization)
131- Smart pointers: `unique_ptr`, `shared_ptr`, `weak_ptr`
132- Custom deleters
133- Rule of Five / Rule of Zero
134- Move semantics
135
136### Concurrency
137- `std::thread` basics
138- Mutex and lock guards
139- `std::async` and `std::future`
140- Condition variables
141- Thread-safe data structures
142- Parallel algorithms
143
144### Design Patterns
145- Singleton (thread-safe Meyer's)
146- Observer with `std::function`
147- Strategy with lambdas
148- CRTP (static polymorphism)
149- Factory pattern
150
151### Templates
152- Function and class templates
153- Template specialization (full and partial)
154- Variadic templates
155- SFINAE (Substitution Failure Is Not An Error)
156- Concepts (C++20)
157- Template template parameters
158- Compile-time computation
159
160### STL
161- Containers: `vector`, `map`, `unordered_map`, `set`
162- Algorithms: `sort`, `transform`, `accumulate`, `find_if`
163- Ranges and views (C++20)
164- Lambda expressions
165- Iterators
166
167## Learning Path
168
1691. **Start here**: `01_modern_cpp.cpp` - Get familiar with modern C++ syntax
1702. **Containers**: `02_stl_containers.cpp` - Learn STL containers and algorithms
1713. **Memory**: `03_smart_pointers.cpp` - Understand modern memory management
1724. **Concurrency**: `04_threading.cpp` - Explore multithreading
1735. **Patterns**: `05_design_patterns.cpp` - Study common design patterns
1746. **Templates**: `06_templates.cpp` - Master template metaprogramming
1757. **Advanced**: `07_move_semantics.cpp` - Deep dive into move semantics
176
177## Compilation Notes
178
179### C++20 Support
180Most examples require C++20. If your compiler doesn't support C++20, you can try C++17:
181
182```bash
183g++ -std=c++17 -Wall -Wextra 01_modern_cpp.cpp -o modern_cpp
184```
185
186Some features will be unavailable:
187- `std::jthread` (C++20)
188- Concepts (C++20)
189- Ranges (C++20)
190- `contains()` for sets (C++20)
191
192### Threading
193Threading examples require linking against pthread on Unix systems:
194
195```bash
196g++ -std=c++20 -pthread 04_threading.cpp -o threading
197```
198
199This is handled automatically by the Makefile.
200
201## Related Resources
202
203- [CPP Learning Guide](/opt/projects/01_Personal/03_Study/content/en/CPP/00_Overview.md)
204- [C Programming Examples](/opt/projects/01_Personal/03_Study/examples/C_Programming/)
205- [cppreference.com](https://en.cppreference.com/) - Comprehensive C++ reference
206
207## License
208
209MIT License - See project root for details