Principle:ClickHouse ClickHouse Strong IP Types
| Knowledge Sources | |
|---|---|
| Domains | Networking, Data_Types |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
A type safety pattern that prevents misuse of IP addresses by wrapping them in distinct types.
Description
Strong IP Types applies the "strong typedef" pattern to network addresses, creating distinct types for IPv4 and IPv6 that cannot be accidentally mixed or misused. This principle prevents common errors such as:
- Passing an IPv4 address where IPv6 is expected
- Using IP addresses as generic integers in unintended contexts
- Forgetting to validate or convert IP addresses
- Buffer overflows from size mismatches
The strong types are zero-cost abstractions - they compile to the same machine code as raw integers but provide compile-time safety.
Usage
Use this principle when:
- Designing APIs that handle multiple address types
- Implementing network protocols
- Storing IP addresses in data structures
- Building type-safe networking libraries
- Preventing address family confusion bugs
Theoretical Basis
Strong Typedef Pattern: Wraps a base type in a new type that is not implicitly convertible, requiring explicit conversions that make intent clear.
Type Safety: The compiler enforces that `IPv4` and `IPv6` cannot be mixed, catching errors at compile time rather than runtime.
Zero-Cost Abstraction: The strong types have no runtime overhead - they are layout-compatible with their underlying types and optimizers eliminate wrapper overhead.
Hash Functions: Custom hash functions enable use in standard containers while accounting for the special semantics of IP addresses (e.g., IPv6 hashes as a 16-byte string view).