-
Move Semantics
Allows transferring resources instead of copying them, improving performance in large data structures.#include <iostream> #include <vector> std::vector<int> createLargeVector() { std::vector<int> v(1000000, 42); return v; // Uses move semantics, avoids expensive copy } int main() { auto data = createLargeVector(); std::cout << "Size: " << data.size() << "\n"; }
-
Smart Pointers
Provides safe memory management usingstd::unique_ptr
andstd::shared_ptr
, avoiding manualdelete
.#include <memory> #include <iostream> struct Foo { Foo() { std::cout << "Foo created\n"; } ~Foo() { std::cout << "Foo destroyed\n"; } }; int main() { std::unique_ptr<Foo> ptr = std::make_unique<Foo>(); // Automatic cleanup, no need for delete }
-
constexpr
for Compile-Time Computation
Enables functions to be evaluated at compile time for improved performance and safety.constexpr int square(int x) { return x * x; } int main() { constexpr int result = square(5); static_assert(result == 25); }
-
Comparison With Rust
Feature C++ Rust Memory Safety Manual / Smart Pointers Borrow Checker + Ownership Concurrency Safety Optional Libraries Built-in with Send
andSync
Compile-Time Const constexpr
const fn
-
Inline Images
Example:
-
Blockquotes for Notes
Pro Tip: Always prefer
std::make_unique
overnew
for exception safety. -
Lists and Sub-Lists
- Move semantics
- RAII (Resource Acquisition Is Initialization)
- Strong type safety
enum class
auto
type deduction
-
Conclusion
Modern C++ provides safer and more expressive constructs while keeping its low-level control. Rust offers memory safety guarantees without garbage collection, making it worth evaluating for new projects.