Header-only C++20 library of carefully-engineered building blocks. Each primitive is small enough to read in one sitting, tested under ASan/UBSan/TSan, and benchmarked against the obvious alternatives.
| Primitive | Header | What it is | Status |
|---|---|---|---|
lru_cache<K,V> |
<sgp/lru_cache.hpp> |
O(1) LRU cache (intrusive DLL + flat hash) | ✅ Ready |
bounded_queue<T> |
<sgp/bounded_queue.hpp> |
Thread-safe bounded MPMC queue (mutex + condvar) | 🚧 Planned |
thread_pool |
<sgp/thread_pool.hpp> |
Fixed-size pool with future-returning submit |
🚧 Planned |
type_erased_function<R(Args...)> |
<sgp/function.hpp> |
Educational std::function clone, SBO-aware |
🚧 Planned |
Most C++ "primitive" libraries are either gigantic (Boost) or unfit for production (LeetCode dumps). This is the in-between: small enough to audit, real enough to use, modern enough to read. I built it as a study artifact for staff-level C++ interviews, then kept it because the code is genuinely useful.
Header-only. Drop include/sgp/ into your project, or via CMake FetchContent:
include(FetchContent)
FetchContent_Declare(
sgp
GIT_REPOSITORY https://github.com/sanjaygulati13/cpp-primitives.git
GIT_TAG main
)
FetchContent_MakeAvailable(sgp)
target_link_libraries(my_app PRIVATE sgp::sgp)#include <sgp/lru_cache.hpp>
#include <iostream>
int main() {
sgp::lru_cache<int, std::string> cache{/*capacity=*/3};
cache.put(1, "one");
cache.put(2, "two");
cache.put(3, "three");
if (auto v = cache.get(1)) {
std::cout << "1 → " << *v << "\n"; // promotes 1 to most-recent
}
cache.put(4, "four"); // evicts 2 (least-recent)
std::cout << "size = " << cache.size() << "\n"; // 3
}See examples/ for more.
- C++20, no warnings on
-Wall -Wextra -Wpedantic -Werror - All tests run under AddressSanitizer + UndefinedBehaviorSanitizer
- Multi-threaded primitives also run under ThreadSanitizer
- clang-tidy with
modernize-*,bugprone-*,performance-*,cppcoreguidelines-* - clang-format enforced in CI
- Benchmarks against
std::list+std::unordered_mapbaseline (seedocs/lru_cache.md)
git clone https://github.com/sanjaygulati13/cpp-primitives.git
cd cpp-primitives
cmake -B build -DSGP_BUILD_TESTS=ON -DSGP_BUILD_BENCHMARKS=ON
cmake --build build
ctest --test-dir build --output-on-failure- Header-only, single-include per primitive. No build complexity for users.
- No exceptions in hot paths. Errors via
std::optionalor status codes. - STL-compatible signatures. Drop-in for educational use.
- One file per primitive. Less than 200 lines wherever possible. Reads like a textbook.
- Tests document behavior. Read
tests/lru_cache_test.cppto understand the contract.
cpp-primitives/
├── include/sgp/ ← header-only library
├── tests/ ← Catch2 unit tests
├── benchmarks/ ← google benchmark micro-benchmarks
├── examples/ ← runnable usage examples
├── docs/ ← per-primitive design rationale + trade-offs
└── .github/workflows/ ← CI: build, sanitize, lint, format-check
This is a personal study project, not actively seeking contributions — but if you spot a bug or a clearer implementation, open an issue or PR. See CONTRIBUTING.md.
MIT. See LICENSE.
C++20 gives concepts, ranges, three-way comparison, designated initializers — modernizations that materially improve primitive design. Backporting to C++17 is possible but I won't; if you need C++17, the implementations are simple enough to port yourself.
Sanjay Gulati (sanjaygulati.dev). Building this as part of a 22-week study cycle. Companion blog posts on each primitive at sanjaygulati.dev/blog.