βοΈ C++ Advanced Concepts
Hereβs where modern, high-performance C++ happens β covering smart pointers, lambdas, move semantics, constexpr, and more.
π§ Smart Pointers (from <memory>)
π‘ Use unique_ptr
for sole ownership. shared_ptr
for shared ownership. Avoid raw new
/delete
.
β‘ Lambda Functions
π Inline anonymous functions β pass them to STL algorithms or callbacks.
π¦ Move Semantics & std::move
π‘ Move avoids deep copies on large objects.
β© constexpr
constexpr int square(int x) { return x * x; }
- Evaluated at compile-time if possible
π decltype & auto
auto n = 5;
β compiler infers int
decltype(n) m = 10;
β same type as n
π¦ Namespaces & Anonymous Namespaces
- Organize code into modules
- Anonymous namespaces give internal linkage (like static globals)
ποΈ RAII (Resource Acquisition Is Initialization)
- Use object lifetime to manage resource cleanup
- Smart pointers + destructors automatically free memory when out of scope
π‘ Modern C++ prefers no raw new
/delete
. Use unique_ptr
, shared_ptr
, and RAII patterns.
π§βπ¬ Bonus: Type Traits & Template Metaprogramming
std::is_integral<T>::value
std::enable_if<...>
- Conditionally enable functions/types at compile time