C++23 modular primitives library -
import mcpplibs.primitives;
| 中文 - English - Forum |
|---|
| 用户文档 - User Documentation |
| API文档 - API Documentation |
This repository provides configurable primitive infrastructure (underlying traits, policy, and operations/dispatcher) to unify numeric behavior, error handling, and concurrency access semantics.
Warning
The project is still evolving quickly, and APIs may change.
- C++23 modules —
import mcpplibs.primitives; - Dual build systems — both xmake and CMake are supported
- Policy-driven behavior — value/type/error/concurrency policies are composable
- Mixed operations support — binary operations between
primitiveandunderlyingare supported - Concurrency access APIs —
primitive::load/store/compare_exchange
The library provides unary, arithmetic, bitwise, and comparison operations for primitive.
Arithmetic paths are dispatched through a unified pipeline and return std::expected<..., policy::error::kind>.
- Value policies (
policy::value::checked/policy::value::saturating/policy::value::unchecked) define overflow behavior. - Error policies (
policy::error::throwing/policy::error::expected/policy::error::terminate) define how errors are propagated.
Example:
import std;
import mcpplibs.primitives;
using namespace mcpplibs::primitives;
using namespace mcpplibs::primitives::operators;
primitive<int> a{1};
primitive<int> b{2};
auto sum = a + b; // std::expected<primitive<int>, policy::error::kind>
using checked_t =
primitive<int, policy::value::checked, policy::error::expected>;
auto maybe_overflow =
checked_t{std::numeric_limits<int>::max()} + checked_t{1};When implementing custom policies, protocol entry points are split by responsibility:
policy::type::handler/policy::type::handler_availablepolicy::concurrency::handler/policy::concurrency::injectionpolicy::value::handler/policy::value::decisionpolicy::error::handler/policy::error::request/policy::error::kind
Built-in policy tags:
policy::value::{checked, unchecked, saturating}policy::type::{strict, compatible, transparent}policy::error::{throwing, expected, terminate}policy::concurrency::{none, fenced, fenced_relaxed, fenced_acq_rel, fenced_seq_cst}
Concurrency notes:
fenced*policies provide operation-level concurrency semantics with injected memory-order fences.primitivestorage keeps a uniform, zero-extra-storage abstraction.primitive::load/store/compare_exchangeare provided by concurrency policy protocols and fail at compile time if unsupported.
Example (concurrent access APIs):
using shared_t = primitive<int, policy::value::checked,
policy::concurrency::fenced_acq_rel,
policy::error::expected>;
shared_t v{1};
v.store(2);
auto expected = 2;
if (v.compare_exchange(expected, 3)) {
auto now = v.load();
(void)now;
}Default policies are available under policy::defaults:
policy::defaults::valuepolicy::defaults::typepolicy::defaults::errorpolicy::defaults::concurrency
ex01_default_arithmetic: Basic arithmetic under default policies.ex02_type_policy: Type negotiation withstrict/compatible, including how type policy affects construction fromunderlying.ex03_value_policy:checked/unchecked/saturatingbehavior, including mixed binary operations withunderlying.ex04_error_policy: Error-handling behavior across different error policies.ex05_concurrency_policy: Representative mixed read/write concurrency workload (writerstore+ readeradd/sub+CAS).ex06_custom_underlying: Custom underlying traits, rep validation, and common-rep extension.ex07_custom_policy: Custom policy protocol implementation.ex08_custom_operation: Custom operation extension.
mcpplibs-primitives/
├── src/ # module sources
│ ├── primitives.cppm # top-level aggregate module
│ ├── primitive/ # primitive definitions and traits
│ ├── policy/ # policy tags and protocol implementations
│ ├── operations/ # operation tags / dispatcher / operators
│ └── underlying/ # underlying traits and common_rep
├── examples/ # ex01 ~ ex08 examples
├── tests/ # test entry and basic test suite
├── xmake.lua # xmake build script
├── CMakeLists.txt # CMake build script
└── .xlings.json # xlings package descriptor
import std;
import mcpplibs.primitives;
int main() {
using namespace mcpplibs::primitives;
using value_t = primitive<int, policy::error::expected>;
auto const result = operations::add(value_t{40}, value_t{2});
return (result.has_value() && result->value() == 42) ? 0 : 1;
}xlings installUsing xmake
xmake build mcpplibs-primitives
xmake run basic # equivalent to ex01_default_arithmetic
xmake run ex05_concurrency_policy
xmake run primitives_testUsing CMake
cmake -B build -G Ninja
cmake --build build --target mcpplibs-primitives
cmake --build build --target ex01_default_arithmetic
cmake --build build --target basic_tests
ctest --test-dir build --output-on-failureadd_repositories("mcpplibs-index https://github.com/mcpplibs/mcpplibs-index.git")
add_requires("primitives")
target("myapp")
set_kind("binary")
set_languages("c++23")
add_files("main.cpp")
add_packages("primitives")
set_policy("build.c++.modules", true)