cpp
Modern C++ programming patterns and idioms
What this skill does
# C++
## Overview
Modern C++ (C++11 and beyond) patterns including RAII, smart pointers, templates, and STL.
---
## Modern C++ Fundamentals
### Smart Pointers
```cpp
#include <memory>
#include <iostream>
// unique_ptr - exclusive ownership
class Resource {
public:
Resource() { std::cout << "Resource acquired\n"; }
~Resource() { std::cout << "Resource released\n"; }
void use() { std::cout << "Resource used\n"; }
};
void unique_ptr_example() {
// Create unique_ptr
auto ptr = std::make_unique<Resource>();
ptr->use();
// Transfer ownership
auto ptr2 = std::move(ptr);
// ptr is now nullptr
// Array support
auto arr = std::make_unique<int[]>(10);
}
// shared_ptr - shared ownership
void shared_ptr_example() {
auto shared1 = std::make_shared<Resource>();
{
auto shared2 = shared1; // Reference count = 2
shared2->use();
} // shared2 destroyed, count = 1
std::cout << "Use count: " << shared1.use_count() << "\n";
} // shared1 destroyed, resource released
// weak_ptr - non-owning reference
class Node {
public:
std::shared_ptr<Node> next;
std::weak_ptr<Node> prev; // Avoid circular reference
~Node() { std::cout << "Node destroyed\n"; }
};
void weak_ptr_example() {
auto node1 = std::make_shared<Node>();
auto node2 = std::make_shared<Node>();
node1->next = node2;
node2->prev = node1; // weak_ptr, no ownership
if (auto locked = node2->prev.lock()) {
// Use locked (shared_ptr)
}
}
```
### RAII Pattern
```cpp
#include <fstream>
#include <mutex>
// File wrapper with RAII
class File {
std::fstream file_;
public:
explicit File(const std::string& filename)
: file_(filename, std::ios::in | std::ios::out) {
if (!file_.is_open()) {
throw std::runtime_error("Failed to open file");
}
}
~File() {
if (file_.is_open()) {
file_.close();
}
}
// Delete copy operations
File(const File&) = delete;
File& operator=(const File&) = delete;
// Allow move operations
File(File&& other) noexcept : file_(std::move(other.file_)) {}
File& operator=(File&& other) noexcept {
file_ = std::move(other.file_);
return *this;
}
void write(const std::string& data) {
file_ << data;
}
};
// Lock guard (RAII for mutex)
class ThreadSafeCounter {
mutable std::mutex mutex_;
int count_ = 0;
public:
void increment() {
std::lock_guard<std::mutex> lock(mutex_);
++count_;
}
int get() const {
std::lock_guard<std::mutex> lock(mutex_);
return count_;
}
};
// Scoped cleanup
template<typename F>
class ScopeGuard {
F cleanup_;
bool active_ = true;
public:
explicit ScopeGuard(F cleanup) : cleanup_(std::move(cleanup)) {}
~ScopeGuard() {
if (active_) cleanup_();
}
void dismiss() { active_ = false; }
ScopeGuard(const ScopeGuard&) = delete;
ScopeGuard& operator=(const ScopeGuard&) = delete;
};
// Usage
void example() {
auto resource = acquireResource();
ScopeGuard guard([&]() { releaseResource(resource); });
// Do work...
guard.dismiss(); // Don't cleanup if successful
}
```
### Move Semantics
```cpp
#include <vector>
#include <string>
#include <utility>
class Buffer {
std::unique_ptr<char[]> data_;
size_t size_;
public:
// Constructor
explicit Buffer(size_t size) : data_(new char[size]), size_(size) {}
// Copy constructor
Buffer(const Buffer& other) : data_(new char[other.size_]), size_(other.size_) {
std::copy(other.data_.get(), other.data_.get() + size_, data_.get());
}
// Move constructor
Buffer(Buffer&& other) noexcept
: data_(std::move(other.data_)), size_(other.size_) {
other.size_ = 0;
}
// Copy assignment
Buffer& operator=(const Buffer& other) {
if (this != &other) {
data_.reset(new char[other.size_]);
size_ = other.size_;
std::copy(other.data_.get(), other.data_.get() + size_, data_.get());
}
return *this;
}
// Move assignment
Buffer& operator=(Buffer&& other) noexcept {
if (this != &other) {
data_ = std::move(other.data_);
size_ = other.size_;
other.size_ = 0;
}
return *this;
}
size_t size() const { return size_; }
};
// Perfect forwarding
template<typename T, typename... Args>
std::unique_ptr<T> make_unique_custom(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
```
---
## Templates
### Function Templates
```cpp
#include <type_traits>
#include <concepts>
// Basic template
template<typename T>
T max(T a, T b) {
return (a > b) ? a : b;
}
// Template specialization
template<>
const char* max<const char*>(const char* a, const char* b) {
return (strcmp(a, b) > 0) ? a : b;
}
// SFINAE (Substitution Failure Is Not An Error)
template<typename T>
typename std::enable_if<std::is_integral<T>::value, T>::type
double_value(T value) {
return value * 2;
}
// C++20 Concepts
template<typename T>
concept Numeric = std::is_arithmetic_v<T>;
template<Numeric T>
T add(T a, T b) {
return a + b;
}
// Requires clause
template<typename T>
requires std::is_default_constructible_v<T>
T create_default() {
return T{};
}
// Variadic templates
template<typename... Args>
void print(Args... args) {
(std::cout << ... << args) << "\n";
}
// Fold expressions
template<typename... Args>
auto sum(Args... args) {
return (args + ...);
}
```
### Class Templates
```cpp
// Generic container
template<typename T, size_t N>
class Array {
T data_[N];
public:
constexpr size_t size() const { return N; }
T& operator[](size_t index) {
if (index >= N) throw std::out_of_range("Index out of range");
return data_[index];
}
const T& operator[](size_t index) const {
if (index >= N) throw std::out_of_range("Index out of range");
return data_[index];
}
T* begin() { return data_; }
T* end() { return data_ + N; }
const T* begin() const { return data_; }
const T* end() const { return data_ + N; }
};
// Template with default arguments
template<typename T, typename Allocator = std::allocator<T>>
class Vector {
// ...
};
// Partial specialization
template<typename T>
class Container<T*> {
// Specialization for pointer types
};
// CRTP (Curiously Recurring Template Pattern)
template<typename Derived>
class Counter {
static inline int count_ = 0;
public:
Counter() { ++count_; }
~Counter() { --count_; }
static int count() { return count_; }
};
class Widget : public Counter<Widget> {
// Widget inherits counting behavior
};
```
---
## STL Containers and Algorithms
```cpp
#include <vector>
#include <map>
#include <unordered_map>
#include <set>
#include <algorithm>
#include <numeric>
void container_examples() {
// vector
std::vector<int> vec{1, 2, 3, 4, 5};
vec.push_back(6);
vec.emplace_back(7); // Construct in place
// map
std::map<std::string, int> ordered_map;
ordered_map["one"] = 1;
ordered_map.insert({"two", 2});
ordered_map.try_emplace("three", 3);
// unordered_map
std::unordered_map<std::string, int> hash_map;
hash_map["one"] = 1;
// set
std::set<int> ordered_set{3, 1, 4, 1, 5};
auto [iter, inserted] = ordered_set.insert(9);
}
void algorithm_examples() {
std::vector<int> vec{5, 2, 8, 1, 9, 3};
// Sort
std::sort(vec.begin(), vec.end());
std::sort(vec.begin(), vec.end(), std::greater<int>());
// Find
auto it = std::find(vec.begin(), vec.end(), 8);
auto it2 = std::find_if(vec.begin(), vec.end(), [](int n) { return n > 5; });
// Transform
std::vector<int> doubled(vec.size());
std::transform(vec.begin(), vec.end(), doubled.begin(), [](int n) { return n * 2; });
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.