# foonathan/type_safe

**Attribution required: if you use, quote, or summarise this content, you must credit and link back to [awesome-repositories.com](https://awesome-repositories.com/repository/foonathan-type-safe).**

1,641 stars · 126 forks · C++ · MIT

## Links

- GitHub: https://github.com/foonathan/type_safe
- Homepage: https://type_safe.foonathan.net
- awesome-repositories: https://awesome-repositories.com/repository/foonathan-type-safe.md

## Topics

`c-plus-plus` `type-safety`

## Description

This library is a collection of zero-overhead C++ abstractions designed to replace primitive types with strict wrappers. By leveraging template metaprogramming, it enforces compile-time type safety and validates data integrity, ensuring that logic errors are caught during the build process rather than at runtime.

The project distinguishes itself by providing a zero-cost abstraction layer that maps high-level type safety wrappers directly to machine instructions. It utilizes static type identity enforcement to treat logically distinct data as incompatible types, even when they share the same underlying representation, and employs compact, union-based containers to manage optional values without heap allocation or metadata overhead.

The library covers a broad range of safety mechanisms, including custom constraints for validating value bounds and specialized wrappers for pointers and indices to prevent null errors. It also provides strict numeric enforcement to prevent accidental data loss or invalid arithmetic, while restricting primitive operations to ensure that variables maintain their intended state throughout the program.

## Tags

### Programming Languages & Runtimes

- [Type Safety](https://awesome-repositories.com/f/programming-languages-runtimes/language-features-paradigms/type-system-tools/type-safety.md) — Provides zero-overhead abstractions that replace primitive types with strict wrappers to catch logic errors at compile time.
- [Static Type Identity Enforcers](https://awesome-repositories.com/f/programming-languages-runtimes/machine-code-generation/intermediate-representations/statically-typed/static-type-identity-enforcers.md) — Treats logically distinct data as incompatible types at compile time to prevent accidental mixing.
- [Numeric Type Utilities](https://awesome-repositories.com/f/programming-languages-runtimes/generic-types/generic-traits/numeric-type-traits/numeric-type-utilities.md) — Provides strict numeric type enforcement to prevent accidental data loss or invalid arithmetic. ([source](https://github.com/foonathan/type_safe#readme))
- [Type Constraint Validation](https://awesome-repositories.com/f/programming-languages-runtimes/generic-types/type-constraint-mapping/type-parameter-constraints/type-constraint-validation.md) — Validates data values against specific bounds or requirements during compilation. ([source](https://github.com/foonathan/type_safe#readme))
- [Compact Union Containers](https://awesome-repositories.com/f/programming-languages-runtimes/memory-efficient-runtimes/memory-efficiency-strategies/compact-union-containers.md) — Implements compact, union-based containers to manage optional values without heap allocation or metadata overhead.
- [Compile-Time Zero-Overhead Validators](https://awesome-repositories.com/f/programming-languages-runtimes/runtime-validator-compilation/compile-time-zero-overhead-validators.md) — Enforces domain-specific constraints and prevents invalid data states without impacting runtime performance.
- [Strong Type Definitions](https://awesome-repositories.com/f/programming-languages-runtimes/strong-type-definitions.md) — Encapsulates raw data within specialized structures to prevent implicit conversions and enforce domain-specific rules.

### Development Tools & Productivity

- [Template Metaprogramming](https://awesome-repositories.com/f/development-tools-productivity/build-tooling/build-time-tooling/template-metaprogramming.md) — Provides a library of advanced template techniques for type-safe alternatives to pointers, integers, and optional values.

### Software Engineering & Architecture

- [Zero-Cost Abstraction Patterns](https://awesome-repositories.com/f/software-engineering-architecture/zero-overhead-abstractions/zero-cost-abstraction-patterns.md) — Maps high-level type safety wrappers directly to machine instructions to ensure no runtime performance penalty.
- [Domain-Driven Type Constraints](https://awesome-repositories.com/f/software-engineering-architecture/data-schema-validation/data-type-validation/domain-driven-type-constraints.md) — Applies specific predicates to data types during compilation to ensure values maintain their intended state and integrity.
- [Domain Type Definitions](https://awesome-repositories.com/f/software-engineering-architecture/data-structures/domain-type-definitions.md) — Enables definition of domain-specific types to prevent null errors and restrict operations to valid data subsets. ([source](https://github.com/foonathan/type_safe#readme))
- [Optional Value Handling](https://awesome-repositories.com/f/software-engineering-architecture/optional-value-handling.md) — Handles data that may or may not exist using memory-efficient containers that ensure safe access.
- [Primitive Operation Restrictors](https://awesome-repositories.com/f/software-engineering-architecture/primitive-type-schemas/primitive-type-restrictions/primitive-operation-restrictors.md) — Restricts primitive operations to ensure variables maintain their intended state throughout the program. ([source](https://github.com/foonathan/type_safe/blob/main/README.md))
- [Domain-Specific](https://awesome-repositories.com/f/software-engineering-architecture/schema-based-type-systems/type-hierarchies/domain-specific.md) — Creates specialized wrappers for data structures to enforce business logic and prevent invalid operations on pointers or indices.

### Testing & Quality Assurance

- [Compile-Time Type Validation](https://awesome-repositories.com/f/testing-quality-assurance/type-assertions/compile-time-type-validation.md) — Verifies data integrity and value bounds at compile time using static assertions and template metaprogramming.

### Part of an Awesome List

- [Numerical Computation](https://awesome-repositories.com/f/awesome-lists/devtools/numerical-computation.md) — Wraps integers and floating point numbers to prevent accidental data loss or invalid arithmetic while maintaining high performance.

### Data & Databases

- [Optional Value Types](https://awesome-repositories.com/f/data-databases/null-value-representations/optional-containers/optional-value-types.md) — Manages optional and variant values using memory-efficient containers and type-safe unions. ([source](https://github.com/foonathan/type_safe#readme))
