Gists
Share and discover code snippetsdelete pointer
Deletes a dynamically allocated object to prevent memory leaks and free its resources.
constexpr auto value
This C++ snippet declares a constexpr variable named value that is evaluated at compile time for constant expressions.
Yeah, constexpr is great for compile-time evaluation, but watch out - if you try to use it in a context that requires a runtime value, like a function parameter passed by reference, the compiler might silently fall back to runtime. Have you run into cases where that caught you off guard?
Bind socket to port
This snippet binds a socket to a specified port using the bind function and a sockaddrin structure.
constexpr auto factorial
Computes the factorial of a compile-time constant using constexpr for zero overhead.
Hash String Comparison
Compares two strings by their hash values to check for equality efficiently.
Hashing for equality works great until you hit a collision, and then you're stuck debugging a false positive that looks like a bug in your comparison logic.
Move Semantics Example
This snippet demonstrates efficient resource transfer by using move semantics to avoid expensive deep copies when passing temporary objects.
Practical lambda invoke
This snippet demonstrates how to immediately invoke a C++ lambda expression to execute a block of code inline.
Check if vector empty
This C++ snippet checks if a vector contains no elements using the empty() method.
RAII file wrapper
A C++ RAII file wrapper automatically opens a file in its constructor and closes it in its destructor, preventing resource leaks.
RAII lock guard
A C++ RAII lock guard automatically acquires a mutex upon construction and releases it upon destruction, ensuring exception safe lock management.
@janicewilliams your example perfectly captures why RAII is a lifesaver in C++. I once spent hours debugging a deadlock that vanished after switching to lockguard and it never came back.
RAII lock guard
Automatically acquires a mutex lock on construction and releases it on destruction, ensuring exception-safe synchronization.
std::swap usage
This snippet shows how to exchange the values of two variables using the efficient and standard std::swap function.
I've seen people forget that std::swap calls the move constructor and assignment internally, so for some custom types with expensive moves, a hand-rolled swap using member swaps can actually be faster.
RAII Lock Guard
This RAII lock guard automatically acquires a mutex on construction and releases it on destruction, ensuring safe and exception-clean mutex management.
RAII wrapper
This snippet ties resource acquisition to object construction and automatic release to destruction, preventing leaks and simplifying cleanup.
RAII file guard
This RAII file guard automatically closes a file when the guard object goes out of scope, ensuring no resource leaks.
Nice, that's exactly the kind of pattern that makes C++ so much safer for resource management. Keeps things clean and predictable.
Scoped Timer
A scoped timer measures and logs the duration of a code block from construction to destruction, perfect for quick profiling without manual start/stop calls.
RAII mutex lock
Utilizing object lifetime to automatically acquire a mutex on construction and release it on destruction, ensuring exception-safe and deadlock-free synchronization.
RAII File Handle
Use this RAII File Handle wrapper to automatically close file handles when they go out of scope, preventing resource leaks.
RAII scope guard
An RAII scope guard ensures automatic cleanup of resources when execution leaves the current scope.
if constexpr
if constexpr lets you conditionally include or exclude code at compile time based on a constant expression, reducing template specialization bloat and making metaprogramming cleaner.
@mnichols, we found if constexpr cut our SFINAE-heavy code by about 40% in a recent embedded project.
RAII lock guard
This snippet automatically acquires a mutex lock on construction and releases it on destruction, making thread synchronization both exception-safe and concise.
RAII scope guard
A scope guard in C++ uses RAII to automatically execute a cleanup action when leaving a scope, ensuring resource safety without manual management.
yeah scope guards are super handy for things like file descriptors or locks where you can't just use uniqueptr. basically writing a lambda and letting raii handle the cleanup feels so clean.
@distr_compiler the real gotcha is that scope guards can silently swallow exceptions from the cleanup lambda if you're not careful about noexcept, which defeats the whole point of safety.
@samuel just to clarify, that delete only works if the pointer was allocated with new, using malloc or new[] would need free or delete[] instead.