gtopt/uid.hpp file

Passkey-gated strong UID type UidOf<Tag> + make_uid factory.

Why this exists

Historically a UID in gtopt was just int32_t, and strong UID aliases like BlockUid, StageUid, PhaseUid, … were strong::type<int32_t, Tag> (via the now-removed StrongUidType template) which did allow brace- construction from a raw integer: BlockUid{3} compiled anywhere. This let test fixtures and production code synthesize UIDs from loop counters — a well-known class of bug, because a UID is an identity token, not a counter, and the N-th row in an array is not in general the element with UID N.

UidOf<Tag> closes that loophole. It has no public constructor taking a uid_t: the only way a user can materialize one from an integer is via the make_uid<Tag>(uid_t) factory, and that factory holds a passkey that nothing else in the codebase can construct. At file/JSON boundaries (where UIDs genuinely cross from raw integers into the type system) code calls make_uid<Block>(parsed_int). Everywhere else, UIDs are copied from existing elements — the compiler rejects fresh synthesis.

What still works

  • Default construction → produces an "unknown" UID (unknown_uid_of<Tag>() is the named form).
  • Copy / move / compare — UidOf<Tag> is regular and ordered.
  • Hashing — usable as a map key.
  • Formatting — usable with std::format / spdlog.
  • Reading the underlying integer — u.value() or implicit conversion to uid_t (kept implicit so existing equality comparisons against raw integers in tests still compile).

What is blocked

  • UidOf<Block>{3} — no matching constructor, hard compile error.
  • UidOf<Block>(3) — same, no matching constructor.
  • static_cast<UidOf<Block>>(3) — same, no conversion path.
  • Cross-tag assignment: UidOf<Block>{stage_uid} — distinct types.

Out of scope

Index strong types (BlockIndex, RowIndex, …) are intentionally left as StrongIndexType / StrongPositionIndexType — they carry arithmetic and positional semantics that the passkey model would make awkward.

Namespaces

namespace gtopt

Classes

template<class Tag, class CharT>
struct std::formatter<gtopt::UidOf<Tag>, CharT>
template<class Tag>
struct std::hash<gtopt::UidOf<Tag>>
class gtopt::uid_passkey
template<class Tag>
class gtopt::UidOf