Implementation:ClickHouse ClickHouse Strong Typedef
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Type_Safety, Patterns |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Template for creating strong typedefs that prevent implicit conversions.
Description
Wraps a type with a distinct tag type, preventing accidental mixing. Includes operators, hash functions, and the STRONG_TYPEDEF macro.
Usage
Use for creating type-safe wrappers (IDs, handles, units) that prevent misuse through implicit conversion.
Code Reference
Source Location
- Repository: ClickHouse
- File: base/base/strong_typedef.h
- Lines: 1-69
Signature
template <typename T, typename Tag>
struct StrongTypedef {
using UnderlyingType = T;
T t;
explicit constexpr StrongTypedef(const T & t_);
operator const T & () const;
T & toUnderType();
const T & toUnderType() const;
};
#define STRONG_TYPEDEF(T, D) \
struct D ## Tag {}; \
using D = StrongTypedef<T, D ## Tag>;
Import
#include <base/strong_typedef.h>
Usage Examples
#include <base/strong_typedef.h>
// Define strong typedefs
STRONG_TYPEDEF(uint64_t, UserID);
STRONG_TYPEDEF(uint64_t, OrderID);
// Usage
UserID user_id(12345);
OrderID order_id(67890);
// Won't compile - prevents mixing
// user_id = order_id; // Error!
// Use in hash maps
std::unordered_map<UserID, std::string> users;
users[user_id] = "Alice";
// Access underlying value
uint64_t raw = user_id.toUnderType();
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment