Type System
STX provides a comprehensive type system built on C++23 fixed-width integers and platform-specific types. These aliases ensure ABI stability, portability, and explicit size semantics across platforms.All STX types are defined in the
lbyte::stx namespace and are based on standard library primitives from <cstdint> and <cstddef>.Unsigned Integers
Fixed-width unsigned integer types guarantee exact bit widths regardless of platform:| Type | Underlying Type | Size | Range |
|---|---|---|---|
u8 | std::uint8_t | 8 bits | 0 to 255 |
u16 | std::uint16_t | 16 bits | 0 to 65,535 |
u32 | std::uint32_t | 32 bits | 0 to 4,294,967,295 |
u64 | std::uint64_t | 64 bits | 0 to 18,446,744,073,709,551,615 |
Usage Example
Signed Integers
Fixed-width signed integer types using two’s complement representation:| Type | Underlying Type | Size | Range |
|---|---|---|---|
i8 | std::int8_t | 8 bits | -128 to 127 |
i16 | std::int16_t | 16 bits | -32,768 to 32,767 |
i32 | std::int32_t | 32 bits | -2,147,483,648 to 2,147,483,647 |
i64 | std::int64_t | 64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Usage Example
Floating Point Types
Standard IEEE 754 floating point types with explicit precision:| Type | Underlying Type | Precision |
|---|---|---|
f32 | float | Single precision (32-bit) |
f64 | double | Double precision (64-bit) |
Usage Example
Size and Pointer Types
Platform-dependent types that match the architecture’s native pointer and size widths:| Type | Underlying Type | Purpose |
|---|---|---|
usize | std::size_t | Unsigned size type for array indexing and memory sizes |
isize | std::ptrdiff_t | Signed size type for pointer differences and offsets |
uptr | std::uintptr_t | Unsigned integer capable of holding any pointer value |
iptr | std::intptr_t | Signed integer capable of holding any pointer value |
On 64-bit systems,
usize, isize, uptr, and iptr are all 64 bits. On 32-bit systems, they are 32 bits.Usage Example
Type System Benefits
Explicit Size Semantics
Explicit Size Semantics
Fixed-width types eliminate platform-specific ambiguities. A
u32 is always 32 bits, whether compiled on ARM, x86, or any other architecture.ABI Stability
ABI Stability
Using explicit-width types ensures binary layouts remain consistent across compiler versions and platforms, critical for binary format parsing.
Readability
Readability
Short, consistent names (
u32 vs unsigned int or uint32_t) improve code clarity without sacrificing precision.Rust-like Ergonomics
Rust-like Ergonomics
Familiar syntax for developers from other systems programming languages while remaining idiomatic C++.
When to Use Each Type
Fixed-Width Types (u8, u16, u32, u64, i8, i16, i32, i64)
Use when:
- Parsing binary formats (PE, ELF, network protocols)
- Interfacing with hardware registers
- Serialization and deserialization
- Cryptographic operations
- Exact bit-width is required
Platform-Sized Types (usize, isize, uptr, iptr)
Use when:
- Array indexing and sizes
- Memory allocation sizes
- Pointer differences
- Address arithmetic
- Container capacities
Compile-Time Guarantees
All type sizes can be verified at compile time:The type system is designed to be constexpr-friendly. All fundamental operations on these types can be evaluated at compile time.
See Also
Strong Types
Learn about type-safe wrappers for addresses and offsets
Zero Overhead
Understand STX’s zero-cost abstraction philosophy