Rust-inspired lightweight utilities for modern C++ projects.
- Option: an optional value type.
- Result: a success-or-error value type.
- Match (C++23): simple pattern-like matching macros.
- Vec helpers (C++23): convenience functions to build vectors.
- C++14 mode: define
INFINITY_LANGUAGE_STD_CPP14 - C++23 mode: define
INFINITY_LANGUAGE_STD_CPP23 - Optional: define
INFINITY_NO_EXCEPTIONS(no exceptions for WASM)
Only one of the language macros should be defined at a time.
-
Option
- Queries:
is_some(),is_none() - Accessors:
unwrap(),unwrap_or(default) - Combinators:
map(F) - C++14 extras:
expect(msg),and_then(F),operator*,operator-> - C++23 helpers:
Nonesentinel,Some(T)factory
- Queries:
-
Result
- Queries:
is_ok(),is_err() - Accessors:
unwrap(),unwrap_err() - Combinators:
map(F) - C++14 extras:
map_err(F),and_then(F),ok(),err()to Option - Utility:
collect_resultsaggregatesContainer<Result<T,E>>intoResult<Container<T>,E>
- Queries:
-
Match (C++23)
MATCH(expr, { SomeCase(x) {...} NoneCase {...} })for OptionMATCH(expr, { OkCase(x) {...} ErrCase(e) {...} })for Result
-
Vec (C++23)
vec_of(a, b, c, ...)vec_repeat(value, n)
This is a header-only library. Copy the include/infinity/ folder into your project and include the headers you need.
Alternatively, add the repository as a submodule and include headers by path.
A minimal CMake project is provided for running the examples in main.cpp.
- Select the mode in
main.cppby defining one of:INFINITY_LANGUAGE_STD_CPP23(default inmain.cpp)INFINITY_LANGUAGE_STD_CPP14- Optionally
INFINITY_NO_EXCEPTIONS
- Configure and build:
cmake -S . -B buildcmake --build build
- Option
using namespace Infinity;
Option<int> o = Some(5);
if (o.is_some()) {
int v = o.unwrap();
}
auto m = o.map([](int x){ return x * 2; });
int d = Option<int>{None}.unwrap_or(42);-
Result
using namespace Infinity; Result<int, std::string> r(123); if (r.is_ok()) { int v = r.unwrap(); } auto rm = r.map([](int x){ return x + 1; }); Result<int, std::string> e(std::string("err")); if (e.is_err()) { std::string s = e.unwrap_err(); }
-
collect_results
using namespace Infinity; std::vector<Result<int, std::string>> v = { Result<int, std::string>(1), Result<int, std::string>(2) }; auto cr = collect_results<int, std::string>(v); // Ok(vector<int>) or Err(std::string)
-
Match
using namespace Infinity; MATCH(o, { SomeCase(v) { /* use v */ break; } NoneCase { /* none */ break; } }); MATCH(r, { OkCase(v) { /* ok */ break; } ErrCase(e) { /* err */ break; } });
-
Vec
using namespace Infinity; auto v1 = vec_of(1,2,3); auto v2 = vec_repeat(std::string("x"), 3);
-
Option
Infinity::Option<int> o = Infinity::Some(5); o.map([](int x){ return x * 2; }); o.and_then([](int x){ return Infinity::Some(x + 1); }); o.unwrap_or(42);
-
Result
auto r = Infinity::Result<int, std::string>::ok(7); r.map([](int x){ return x + 1; }); r.map_err([](const std::string& e){ return e + "!"; }); r.and_then([](int x){ return Infinity::Result<int, std::string>::ok(x * 2); });
- Keep
MATCHblocks with explicitbreakstatements to avoid fallthrough in macro-generated loops. collect_resultspreserves the input container type and reserves capacity when possible (e.g.,std::vector).- In C++23 mode,
SomeandNoneare inline helpers. In C++14 mode, useInfinity::Some()and theInfinity::Noneconstant.