
Cpp Pro Skill
- 122 installs
- 404kidwiz/claude-supercode-skills
Write performance-critical systems, low-level libraries, and high-concurrency applications in C++.
About
Master C++ for professional systems programming. Covers modern C++ standards (C++17+), memory management strategies, concurrency patterns, and how to write high-performance services and libraries.
- Modern C++ standards
- Memory management
- High-performance systems
- Concurrency patterns
- Low-level optimization
Cpp Pro by the numbers
- 122 all-time installs (skills.sh)
- Ranked #2,909 of 4,492 Backend & APIs skills by installs in the Skillselion catalog
- Data as of Aug 11, 2026 (Skillselion catalog sync)
npx skills add https://github.com/404kidwiz/claude-supercode-skills --skill cpp-proAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 122 |
|---|---|
| Repository | 404kidwiz/claude-supercode-skills ↗ |
What it does
Write performance-critical systems, low-level libraries, and high-concurrency applications in C++.
Files
C++ Professional
Purpose
Provides expert C++20 programming capabilities specializing in modern C++ features (concepts, modules, ranges, coroutines), performance optimization, and system-level programming. Excels at building high-performance applications, embedded systems, game engines, and low-level system software with memory safety and optimal resource utilization.
When to Use
- Building high-performance applications requiring C++ speed (game engines, simulations)
- Implementing system-level software (device drivers, operating systems, embedded systems)
- Optimizing performance-critical code (SIMD, cache optimization, lock-free programming)
- Migrating legacy C++ codebases to modern C++20 standards
- Building cross-platform C++ libraries and SDKs
- Implementing template metaprogramming and compile-time optimizations
- Working with modern C++20 features (concepts, modules, ranges, coroutines)
Quick Start
Invoke this skill when:
- Building high-performance C++ applications (games, simulations, trading)
- System-level programming (device drivers, embedded systems, OS)
- Performance optimization (SIMD, cache, lock-free)
- Modern C++20 features (concepts, modules, ranges, coroutines)
- Template metaprogramming and compile-time computation
- Cross-platform library development
Do NOT invoke when:
- Web development → Use frontend-developer or backend-developer
- Scripting tasks → Use python-pro or javascript-pro
- Simple utilities without performance needs → Use appropriate language
- Mobile development → Use swift-expert or kotlin-specialist
Core Capabilities
C++20 Modern Features
- Concepts: Type constraints and template requirements
- Modules: Replacing header files with importable modules
- Ranges: Lazy evaluation algorithms and views
- Coroutines: Asynchronous programming with co_await
- Spaceship Operator: Three-way comparison <=>
- Designated Initializers: Struct member initialization by name
- std::format: Type-safe string formatting
- std::span: Safe array views without ownership
- std::jthread: Thread with automatic join capability
Performance Optimization
- Template Metaprogramming: Compile-time computation
- SIMD Programming: Vector instructions for parallel processing
- Memory Management: Smart pointers, allocators, memory pools
- Cache-Aware Algorithms: Data-oriented design patterns
- Lock-Free Programming: Atomic operations and memory ordering
- Compiler Optimizations: Profile-guided optimization, link-time optimization
System Programming
- Low-Level I/O: File descriptors, sockets, epoll/kqueue
- Memory Mapping: Shared memory, memory-mapped files
- Process Management: Fork, exec, signal handling
- System Calls: POSIX/Linux system interface
- Embedded Systems: Bare-metal programming, real-time constraints
Decision Framework
C++ Feature Selection
C++20 Feature Decision
├─ Type constraints needed
│ └─ Use concepts instead of SFINAE
│ • Clearer error messages
│ • More readable templates
│
├─ Header file management
│ └─ Use modules for new projects
│ • Faster compilation
│ • Better encapsulation
│
├─ Data transformations
│ └─ Use ranges for lazy evaluation
│ • Composable algorithms
│ • Memory efficient
│
├─ Async operations
│ └─ Use coroutines for I/O-bound work
│ • Efficient state machines
│ • Readable async code
│
└─ Error handling
├─ Recoverable errors → std::expected
├─ Exceptional cases → exceptions
└─ Low-level code → return codesPerformance Optimization Matrix
| Bottleneck | Solution | Complexity |
|---|---|---|
| CPU-bound computation | SIMD, parallelism | High |
| Memory allocation | Memory pools, allocators | Medium |
| Cache misses | Data-oriented design | High |
| Lock contention | Lock-free structures | Very High |
| Compilation time | Modules, precompiled headers | Low |
Best Practices
Modern C++ Development
- Prefer Composition to Inheritance: Use value semantics and composition
- const Correctness: Mark member functions const when possible
- noexcept When Appropriate: Mark functions that won't throw
- Explicit is Better: Use explicit constructors and conversion operators
- RAII Everywhere: Wrap all resources in RAII objects
Performance Optimization
- Profile Before Optimizing: Use perf, VTune, or Tracy
- Rule of Zero: Define destructors, copy, and move only if needed
- Move Semantics: Return by value, rely on move semantics
- Inline Judiciously: Let compiler decide; focus on cache-friendly data
- Measure Cache Efficiency: Cache misses are often more expensive
Template Metaprogramming
- Concepts Over SFINAE: Use concepts for clearer template constraints
- constexpr When Possible: Move computation to compile time
- Type Traits: Use std::type_traits for compile-time introspection
- Variadic Templates: Use parameter packs for flexible functions
Concurrency and Parallelism
- Avoid Premature Locking: Consider lock-free for high-contention
- Understand Memory Ordering: Use std::memory_order explicitly
- Future/Promise Patterns: Use std::future for async results
- Coroutines for I/O: Use C++20 coroutines for async I/O
- Thread Pools: Prefer pools over spawning threads
System-Level Programming
- Zero-Cost Abstractions: High-level code that compiles efficiently
- Handle Errors Explicitly: Use std::expected without exceptions
- Resource Management: Apply RAII consistently
- Platform Abstraction: Isolate platform-specific code
- Testing Strategy: Use unit tests, fuzzing, property-based testing
Anti-Patterns
Memory Management
- Raw new/delete: Use smart pointers instead
- Manual Resource Management: Apply RAII
- Dangling Pointers: Use ownership semantics
Performance
- Premature Optimization: Profile first
- Virtual Call Overhead: Use CRTP when performance critical
- Unnecessary Copies: Use move semantics and references
Code Organization
- Header-Only Everything: Use modules or proper compilation units
- Macro Abuse: Use constexpr, templates, inline functions
- Global State: Use dependency injection
Additional Resources
- Detailed Technical Reference: See REFERENCE.md
- Code Examples & Patterns: See EXAMPLES.md
C++ Professional - Code Examples & Patterns
Lock-Free Queue Implementation
// Lock-free queue for high-performance scenarios
template<typename T>
class LockFreeQueue {
public:
struct Node {
std::atomic<Node*> next{nullptr};
T data;
template<typename... Args>
explicit Node(Args&&... args) : data(std::forward<Args>(args)...) {}
};
LockFreeQueue() : dummy_(new Node{}), head_(&dummy_), tail_(&dummy_) {}
~LockFreeQueue() {
while (Node* head = head_.load()) {
head_.store(head->next);
delete head;
}
}
void enqueue(T value) {
Node* new_node = new Node{std::move(value)};
Node* prev_tail = tail_.exchange(new_node);
prev_tail->next.store(new_node);
}
bool dequeue(T& result) {
Node* head = head_.load();
Node* next = head->next.load();
if (!next) return false;
result = std::move(next->data);
head_.store(next);
delete head;
return true;
}
private:
Node* dummy_;
std::atomic<Node*> head_;
std::atomic<Node*> tail_;
};
using OrderQueue = LockFreeQueue<Order>;Custom Smart Pointer with Pool Allocation
template<typename T>
class PooledPtr {
public:
explicit PooledPtr(MemoryPool<T>& pool, T* ptr = nullptr)
: pool_(&pool), ptr_(ptr) {}
~PooledPtr() {
if (ptr_) {
ptr_->~T();
pool_->deallocate(ptr_, 1);
}
}
PooledPtr(const PooledPtr&) = delete;
PooledPtr& operator=(const PooledPtr&) = delete;
PooledPtr(PooledPtr&& other) noexcept
: pool_(other.pool_), ptr_(other.ptr_) {
other.ptr_ = nullptr;
}
PooledPtr& operator=(PooledPtr&& other) noexcept {
if (this != &other) {
reset();
pool_ = other.pool_;
ptr_ = other.ptr_;
other.ptr_ = nullptr;
}
return *this;
}
template<typename... Args>
static PooledPtr make(MemoryPool<T>& pool, Args&&... args) {
T* ptr = pool.allocate(1);
new (ptr) T(std::forward<Args>(args)...);
return PooledPtr(pool, ptr);
}
T& operator*() const { return *ptr_; }
T* operator->() const { return ptr_; }
T* get() const noexcept { return ptr_; }
void reset() {
if (ptr_) {
ptr_->~T();
pool_->deallocate(ptr_, 1);
ptr_ = nullptr;
}
}
explicit operator bool() const noexcept { return ptr_ != nullptr; }
private:
MemoryPool<T>* pool_;
T* ptr_;
};Testing with Google Test
// order_service_test.cpp
#include <gtest/gtest.h>
#include "order_service.hpp"
#include "mock_database.hpp"
using namespace order_system;
class OrderServiceTest : public ::testing::Test {
protected:
void SetUp() override {
mock_db_ = std::make_shared<MockDatabase>();
service_ = std::make_unique<OrderService<MockDatabase>>(mock_db_);
}
std::shared_ptr<MockDatabase> mock_db_;
std::unique_ptr<OrderService<MockDatabase>> service_;
};
TEST_F(OrderServiceTest, CreateOrder_Success) {
// Arrange
const std::string customer_id = "customer_123";
const std::vector<OrderItem> items{
{"product_1", 2, 29.99},
{"product_2", 1, 49.99}
};
EXPECT_CALL(*mock_db_, insert_order(::testing::_))
.WillOnce(::testing::Return(true));
// Act
auto result = service_->create_order_with_total_check(customer_id, items, 200.0);
// Assert
ASSERT_TRUE(result.has_value());
EXPECT_EQ(result.value()->customer_id(), customer_id);
EXPECT_EQ(result.value()->items().size(), 2);
EXPECT_DOUBLE_EQ(result.value()->total_amount(), 109.97);
}
TEST_F(OrderServiceTest, CreateOrder_ExceedsMaxTotal) {
// Arrange
const std::string customer_id = "customer_123";
const std::vector<OrderItem> items{
{"product_1", 10, 100.00}
};
// Act
auto result = service_->create_order_with_total_check(customer_id, items, 50.0);
// Assert
ASSERT_FALSE(result.has_value());
EXPECT_STREQ(result.error().c_str(), "Order total exceeds maximum allowed amount");
}
// Property-based testing with custom generators
class OrderPropertyTest : public ::testing::Test {
protected:
static auto generate_random_items(int count) -> std::vector<OrderItem> {
std::vector<OrderItem> items;
items.reserve(count);
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> qty_dist(1, 10);
std::uniform_real_distribution<> price_dist(0.01, 1000.0);
for (int i = 0; i < count; ++i) {
items.emplace_back(
"product_" + std::to_string(i),
qty_dist(gen),
price_dist(gen)
);
}
return items;
}
};
TEST_F(OrderPropertyTest, TotalCalculation_Consistency) {
for (int test = 0; test < 100; ++test) {
const auto items = generate_random_items(10);
// Test SIMD vs scalar calculation
OrderProcessor processor;
const double simd_total = processor.calculate_total_simd(items);
const double scalar_total = std::accumulate(items.begin(), items.end(), 0.0,
[](double acc, const OrderItem& item) {
return acc + item.total();
});
EXPECT_DOUBLE_EQ(simd_total, scalar_total) << "Test iteration: " << test;
}
}
// Performance benchmarks
TEST(OrderBenchmark, ParallelProcessing) {
std::vector<Order> orders;
orders.reserve(10000);
for (int i = 0; i < 10000; ++i) {
orders.emplace_back(
"customer_" + std::to_string(i),
std::vector<OrderItem>{{"product_1", 1, 29.99}}
);
}
OrderProcessor processor(4);
auto start = std::chrono::high_resolution_clock::now();
processor.process_orders_parallel(orders);
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
std::cout << "Parallel processing took: " << duration.count() << "ms\n";
// Verify all orders were processed
EXPECT_TRUE(std::all_of(orders.begin(), orders.end(),
[](const Order& order) {
return order.status() == OrderStatus::Completed;
}));
}Example Use Cases
Example 1: High-Performance Trading Engine
Scenario: Building a low-latency financial trading engine requiring microsecond-level response times.
Implementation Approach: 1. Lock-Free Architecture: Used atomic operations and memory ordering for zero-contention data paths 2. SIMD Optimization: Implemented vectorized price calculations using AVX-512 3. Cache Optimization: Designed data structures for cache-line alignment and prefetching 4. Coroutine-Based Concurrency: Used C++20 coroutines for efficient I/O multiplexing
Performance Results:
- Order processing latency: 50μs (down from 500μs)
- Throughput: 100K orders/second (up from 20K)
- CPU utilization: Reduced by 40% through better cache locality
Example 2: Embedded Real-Time Controller
Scenario: Developing firmware for a medical device with hard real-time constraints (< 1ms response).
Implementation Strategy: 1. Zero-Allocation Design: Pre-allocated memory pools, no dynamic allocation in hot paths 2. constexpr Everything: Compile-time computation for configuration and validation 3. Concepts-Based API: Type-safe interfaces preventing misuse at compile time 4. Hardware Abstraction: Portable layer supporting multiple microcontroller platforms
Key Techniques:
// Compile-time validated configuration
template<RealTimeSystem T>
class Controller {
static_assert(T::max_latency_ms < 1, "Latency requirement not met");
// Pre-allocated buffer pools
std::array<Message, 256> message_pool_;
std::atomic_size_t pool_index_{0};
};Example 3: Cross-Platform Game Engine Library
Scenario: Creating a game engine SDK that compiles to Windows, macOS, Linux, and consoles.
Architecture Decisions: 1. Module-Based Build: Using C++20 modules for faster compilation and cleaner interfaces 2. Concept Constraints: Ensuring platform-specific code meets interface requirements 3. Modern RAII: Resource management through smart pointers and RAII wrappers 4. Error Handling: Using std::expected for recoverable errors without exceptions
Results:
- Compile time reduction: 45% through modules
- Cross-platform compatibility: 95% shared code
- Memory safety: Zero memory-related CVEs in 2 years
Concepts Example
// Using concepts for type-safe templates
template<typename T>
concept Serializable = requires(T t, std::ostream& os) {
{ t.serialize(os) } -> std::same_as<void>;
{ T::deserialize(std::declval<std::istream&>()) } -> std::same_as<T>;
};
template<Serializable T>
void save_to_file(const T& obj, const std::string& filename) {
std::ofstream file(filename);
obj.serialize(file);
}
// Compound concepts
template<typename T>
concept OrderComponent = Serializable<T> && requires(T t) {
{ t.id() } -> std::convertible_to<std::string>;
{ t.validate() } -> std::same_as<bool>;
};Ranges Example
// Modern ranges-based data processing
auto get_top_orders_by_value(const std::vector<Order>& orders, size_t n) {
return orders
| std::views::filter([](const Order& o) {
return o.status() == OrderStatus::Completed;
})
| std::views::transform([](const Order& o) {
return std::make_pair(o.id(), o.total_amount());
})
| std::ranges::to<std::vector>()
| std::ranges::actions::sort([](auto& a, auto& b) {
return a.second > b.second;
})
| std::views::take(n);
}
// Lazy evaluation with views
auto pending_high_value = orders
| std::views::filter([](const Order& o) {
return o.status() == OrderStatus::Pending && o.total_amount() > 1000;
});
// Only evaluate when needed
for (const auto& order : pending_high_value | std::views::take(10)) {
process(order);
}std::format Example
// Type-safe formatting with std::format
std::string format_order_summary(const Order& order) {
return std::format(
"Order #{} | Customer: {} | Items: {} | Total: ${:.2f} | Status: {}",
order.id(),
order.customer_id(),
order.items().size(),
order.total_amount(),
magic_enum::enum_name(order.status())
);
}
// Format with alignment
std::string format_table_row(std::string_view name, double value) {
return std::format("{:<20} {:>10.2f}", name, value);
}
// Format with localization
std::string format_with_locale(double amount) {
return std::format(std::locale("en_US.UTF-8"), "{:L}", amount);
}C++ Professional - Technical Reference
Architecture Patterns
Modern C++ Project Structure
// order.hpp - Interface
#pragma once
#include <uuid/uuid.h>
#include <vector>
#include <memory>
#include <chrono>
#include <expected>
#include <string_view>
#include <ranges>
#include <concepts>
namespace order_system {
enum class OrderStatus : uint8_t {
Pending = 0,
Processing = 1,
Completed = 2,
Cancelled = 3
};
template<typename T>
concept Numeric = std::is_arithmetic_v<T>;
struct OrderItem {
std::string product_id;
int quantity;
double unit_price;
constexpr double total() const noexcept {
return quantity * unit_price;
}
};
class Order {
public:
Order(std::string customer_id, std::vector<OrderItem> items);
[[nodiscard]] const std::string& id() const noexcept { return id_; }
[[nodiscard]] const std::string& customer_id() const noexcept { return customer_id_; }
[[nodiscard]] const std::vector<OrderItem>& items() const noexcept { return items_; }
[[nodiscard]] OrderStatus status() const noexcept { return status_; }
[[nodiscard]] double total_amount() const noexcept;
void set_status(OrderStatus status) noexcept { status_ = status; }
[[nodiscard]] std::chrono::system_clock::time_point created_at() const noexcept { return created_at_; }
// Constexpr constructor for compile-time creation
static constexpr Order create_empty() noexcept {
return Order{};
}
private:
Order() = default; // Private for create_empty
std::string id_;
std::string customer_id_;
std::vector<OrderItem> items_;
OrderStatus status_ = OrderStatus::Pending;
std::chrono::system_clock::time_point created_at_;
};
// Factory function with proper error handling
using OrderResult = std::expected<std::unique_ptr<Order>, std::string>;
OrderResult create_order(std::string_view customer_id,
std::span<const OrderItem> items);
} // namespace order_systemTemplate-Based Service Layer
// order_service.hpp
#pragma once
#include "order.hpp"
#include <database/connection_pool.hpp>
#include <cache/lru_cache.hpp>
#include <concepts>
#include <mutex>
#include <shared_mutex>
namespace order_system {
template<typename DatabaseConnection>
requires requires(DatabaseConnection conn) {
{ conn.execute_query(std::string_view{}) } -> std::same_as<std::vector<std::unordered_map<std::string, std::string>>>;
{ conn.execute_update(std::string_view{}) } -> std::same_as<int>;
}
class OrderService {
public:
explicit OrderService(std::shared_ptr<DatabaseConnection> db,
size_t cache_size = 1000)
: db_(std::move(db))
, cache_(cache_size) {}
// Modern function with concepts
template<Numeric TotalType>
auto create_order_with_total_check(const std::string& customer_id,
std::vector<OrderItem> items,
TotalType max_total) -> OrderResult {
const auto total = calculate_total(items);
if (total > max_total) {
return std::unexpected("Order total exceeds maximum allowed amount");
}
return create_order_internal(customer_id, std::move(items));
}
// Ranges-based processing
auto get_orders_by_total_range(double min_total, double max_total) const
-> std::vector<Order> {
return orders_
| std::views::filter([&](const Order& order) {
return order.total_amount() >= min_total &&
order.total_amount() <= max_total;
})
| std::ranges::to<std::vector>();
}
// Compile-time string formatting with std::format
std::string generate_order_report(const Order& order) const {
return std::format(
"Order Report\n"
"============\n"
"Order ID: {}\n"
"Customer: {}\n"
"Status: {}\n"
"Items: {}\n"
"Total: ${:.2f}\n"
"Created: {:%Y-%m-%d %H:%M:%S}",
order.id(),
order.customer_id(),
static_cast<int>(order.status()),
order.items().size(),
order.total_amount(),
order.created_at()
);
}
private:
OrderResult create_order_internal(const std::string& customer_id,
std::vector<OrderItem> items);
static constexpr double calculate_total(std::span<const OrderItem> items) noexcept {
return std::accumulate(items.begin(), items.end(), 0.0,
[](double acc, const OrderItem& item) {
return acc + item.total();
});
}
std::shared_ptr<DatabaseConnection> db_;
mutable LRUCache<std::string, Order> cache_;
mutable std::shared_mutex cache_mutex_;
std::vector<Order> orders_;
};
// Type alias for common use case
using PostgreSQLOrderService = OrderService<PostgreSQLConnection>;
} // namespace order_systemHigh-Performance Data Processing
// order_processor.hpp
#pragma once
#include "order.hpp"
#include <vector>
#include <thread>
#include <atomic>
#include <barrier>
#include <latch>
#include <algorithm>
#include <execution>
namespace order_system {
class OrderProcessor {
public:
explicit OrderProcessor(size_t num_threads = std::thread::hardware_concurrency())
: num_threads_(num_threads), stop_flag_(false) {}
// Parallel processing with std::execution policies
void process_orders_parallel(std::vector<Order>& orders) {
std::for_each(std::execution::par_unseq, orders.begin(), orders.end(),
[](Order& order) {
if (order.status() == OrderStatus::Pending) {
process_single_order(order);
}
});
}
// SIMD-accelerated total calculation
struct alignas(32) OrderData {
double quantities[4];
double prices[4];
double totals[4];
};
double calculate_total_simd(std::span<const OrderItem> items) {
if (items.size() < 4) {
return calculate_total_scalar(items);
}
double total = 0.0;
const size_t simd_chunks = (items.size() / 4) * 4;
// Process 4 items at a time using SIMD
for (size_t i = 0; i < simd_chunks; i += 4) {
OrderData data;
for (size_t j = 0; j < 4; ++j) {
data.quantities[j] = items[i + j].quantity;
data.prices[j] = items[i + j].unit_price;
}
// SIMD multiplication
for (size_t j = 0; j < 4; ++j) {
data.totals[j] = data.quantities[j] * data.prices[j];
}
total += std::accumulate(std::begin(data.totals), std::end(data.totals), 0.0);
}
// Process remaining items
for (size_t i = simd_chunks; i < items.size(); ++i) {
total += items[i].total();
}
return total;
}
private:
static void process_single_order(Order& order) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
order.set_status(OrderStatus::Processing);
order.set_status(OrderStatus::Completed);
}
static double calculate_total_scalar(std::span<const OrderItem> items) {
return std::accumulate(items.begin(), items.end(), 0.0,
[](double acc, const OrderItem& item) {
return acc + item.total();
});
}
size_t num_threads_;
std::vector<std::thread> workers_;
std::atomic<bool> stop_flag_;
};
} // namespace order_systemMemory Management
Memory Pool Implementation
// memory_pool.hpp
#pragma once
#include <memory>
#include <vector>
#include <cstddef>
#include <mutex>
namespace order_system {
template<typename T, size_t BlockSize = 1024>
class MemoryPool {
public:
MemoryPool() : blocks_(), free_list_(nullptr) {}
~MemoryPool() {
for (auto block : blocks_) {
::operator delete(block);
}
}
T* allocate(size_t n) {
if (n != 1) {
return static_cast<T*>(::operator new(n * sizeof(T)));
}
std::lock_guard<std::mutex> lock(mutex_);
if (!free_list_) {
allocate_block();
}
T* result = reinterpret_cast<T*>(free_list_);
free_list_ = free_list_->next;
return result;
}
void deallocate(T* ptr, size_t n) noexcept {
if (n != 1) {
::operator delete(ptr);
return;
}
std::lock_guard<std::mutex> lock(mutex_);
Node* node = reinterpret_cast<Node*>(ptr);
node->next = free_list_;
free_list_ = node;
}
private:
struct Node {
Node* next;
};
void allocate_block() {
char* block = static_cast<char*>(::operator new(BlockSize * sizeof(T)));
blocks_.push_back(block);
for (size_t i = 0; i < BlockSize; ++i) {
Node* node = reinterpret_cast<Node*>(block + i * sizeof(T));
node->next = free_list_;
free_list_ = node;
}
}
std::vector<void*> blocks_;
Node* free_list_;
std::mutex mutex_;
};
} // namespace order_systemBuild System Configuration
CMake with Modern C++20
# CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(OrderSystem VERSION 1.0.0 LANGUAGES CXX)
# Set C++20 standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Compiler-specific optimizations
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
target_compile_options(order_system PRIVATE -O3 -march=native -flto)
target_link_options(order_system PRIVATE -flto)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
target_compile_options(order_system PRIVATE /O2 /GL)
target_link_options(order_system PRIVATE /LTCG)
endif()
# Find required packages
find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)
find_package(fmt REQUIRED)
# Create main library
add_library(order_system
src/order.cpp
src/order_service.cpp
src/order_processor.cpp
)
target_include_directories(order_system
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(order_system
PUBLIC
fmt::fmt
Threads::Threads
)
# Add tests
include(CTest)
enable_testing()
add_subdirectory(tests)
# Installation
include(GNUInstallDirs)
install(TARGETS order_system
EXPORT OrderSystemTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)Coroutines for Asynchronous Operations
#include <coroutine>
#include <future>
namespace order_system {
template<typename T>
struct Promise {
T value_;
std::coroutine_handle<> continuation_;
bool ready_ = false;
OrderSystemTask get_return_object() {
return OrderSystemTask{std::coroutine_handle<Promise>::from_promise(*this)};
}
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept {
if (continuation_) {
continuation_.resume();
}
return {};
}
void return_value(T value) {
value_ = std::move(value);
ready_ = true;
}
void unhandled_exception() { throw; }
struct awaiter {
Promise* promise_;
bool await_ready() { return promise_->ready_; }
void await_suspend(std::coroutine_handle<> continuation) {
promise_->continuation_ = continuation;
}
T await_resume() { return std::move(promise_->value_); }
};
};
template<typename T>
struct OrderSystemTask {
using promise_type = Promise<T>;
std::coroutine_handle<promise_type> coro_;
~OrderSystemTask() { if (coro_) coro_.destroy(); }
// Move constructor/assignment
OrderSystemTask(OrderSystemTask&& other) noexcept : coro_(other.coro_) {
other.coro_ = {};
}
OrderSystemTask& operator=(OrderSystemTask&& other) noexcept {
if (this != &other) {
if (coro_) coro_.destroy();
coro_ = other.coro_;
other.coro_ = {};
}
return *this;
}
auto operator co_await() {
return typename promise_type::awaiter{&coro_.promise()};
}
};
// Usage example
OrderSystemTask<std::vector<Order>> process_orders_async(
std::shared_ptr<DatabaseConnection> db,
std::vector<std::string> customer_ids) {
std::vector<Order> results;
results.reserve(customer_ids.size());
for (const auto& customer_id : customer_ids) {
auto orders = co_await db->get_orders_by_customer(customer_id);
results.insert(results.end(), orders.begin(), orders.end());
}
co_return results;
}
} // namespace order_system