0 day streak
Code library

Code worth reading.

Hand-picked snippets across TypeScript, Python, Rust, Go, and more — each one comes with a line-by-line breakdown so you can read it like the people who wrote it.

3495 snippets · 10 languages
▶ Start learning
C++ · IntermediateSnippet

Move Semantics with std::move

Move semantics allow resources (like heap memory) to be moved from one object to another instead of being copied. std::move casts an object to an rvalue, enabling the move constructor to take over the data efficiently.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>
#include <utility>
int main() {
std::vector<int> vec1 = {1, 2, 3, 4, 5};
// Transfer ownership of resources from vec1 to vec2
std::vector<int> vec2 = std::move(vec1);
std::cout << "vec2 size: " << vec2.size() << std::endl;
std::cout << "vec1 size: " << vec1.size() << std::endl; // Likely 0
return 0;
}
PerformanceMemory Management
C++ · IntermediateSnippet

Smart Pointers: std::unique_ptr

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. It cannot be copied, only moved, ensuring strict single ownership.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
#include <memory>
#include <iostream>
void example() {
// Creating a unique_ptr to an integer
std::unique_ptr<int> ptr = std::make_unique<int>(42);
if (ptr) {
std::cout << "Value: " << *ptr << std::endl;
}
} // ptr is automatically destroyed here, and memory is freed
Memory ManagementBest Practices
C++ · IntermediateSnippet

Lambda Expressions and Capturing

Lambdas are anonymous functions that can be defined inline. The capture clause '[]' allows the lambda to access variables from the surrounding scope, either by value or by reference.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
int threshold = 10;
auto isAbove = [threshold](int val) {
return val > threshold;
};
std::cout << std::boolalpha << isAbove(15) << std::endl;
return 0;
}
FunctionsSyntax
C++ · IntermediateSnippet

Virtual Destructors in Inheritance

In C++, if you delete a derived class object through a base class pointer, the behavior is undefined unless the base class has a virtual destructor. Marking it virtual ensures the entire object hierarchy is cleaned up correctly.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Base {
public:
virtual ~Base() { /* Cleanup base */ }
};
class Derived : public Base {
int* data;
public:
Derived() { data = new int[10]; }
~Derived() override { delete[] data; }
};
void cleanup() {
Base* ptr = new Derived();
delete ptr; // Calls Derived destructor because Base destructor is virtual
}
OOPMemory Management
C++ · IntermediateSnippet

RAII and Exception Safety

Resource Acquisition Is Initialization (RAII) is a core C++ idiom. Resources are tied to object lifetime; they are acquired in the constructor and released in the destructor, ensuring they are never leaked, even during exceptions.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <mutex>
std::mutex mtx;
void shared_resource_access() {
// Mutex is locked in constructor
std::lock_guard<std::mutex> lock(mtx);
// Critical section: performing work
if (/* something fails */ false) {
throw std::runtime_error("Error");
}
} // Mutex is released in destructor, even if exception occurs
Memory ManagementBest Practices
C++ · IntermediateSnippet

Template Specialization

Template Specialization allows you to define a custom implementation of a template for a specific data type, overriding the generic version.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
template <typename T>
class Formatter {
public:
void print(T val) { /* Default logic */ }
};
template <>
class Formatter<bool> {
public:
void print(bool val) {
std::cout << (val ? "Yes" : "No");
}
};
SyntaxPerformance
C++ · IntermediateSnippet

Operator Overloading

Operator overloading lets you redefine how standard operators like + or - behave when used with your custom classes.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
9
class Vector2D {
public:
float x, y;
Vector2D operator+(const Vector2D& other) const {
return {x + other.x, y + other.y};
}
};
Vector2D v3 = v1 + v2;
OOPFunctions
C++ · IntermediateSnippet

The 'mutable' Keyword

The mutable keyword allows a specific member of a class to be modified even if the object is accessed through a const reference or inside a const member function.

snippet.cpp
cpp
1
2
3
4
5
6
7
8
class Image {
mutable bool cacheValid = false;
public:
void render() const {
// Perform heavy rendering...
cacheValid = true; // Allowed despite const function
}
};
OOPSyntax