Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:ClickHouse ClickHouse Strong Typedef

From Leeroopedia
Revision as of 14:38, 16 February 2026 by Admin (talk | contribs) (Auto-imported from implementations/ClickHouse_ClickHouse_Strong_Typedef.md)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


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

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