JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
Loading...
Searching...
No Matches
tuple_like.h File Reference

Validation utilities for tuple-like traits, comprising structural detection of tuple-like semantics and strict matching for pair-like binary tuple structures. More...

#include <tuple>
#include <utility>
#include <type_traits>
#include <concepts>

Go to the source code of this file.

Namespaces

namespace  jh::concepts
 Behavioral concept namespace of the JH Toolkit.

Concepts

concept  jh::concepts::tuple_like
 Concept recognizing tuple-like types.
concept  jh::concepts::pair_like_for
 Checks whether a type P is a 2-element tuple-like whose element types exactly match K and V (after remove_cvref).

Detailed Description

Validation utilities for tuple-like traits, comprising structural detection of tuple-like semantics and strict matching for pair-like binary tuple structures.

Author
JeongHan-Bae <mastropseudo@gmail.com>

This header provides compile-time mechanisms that verify whether a type fulfills the requirements of the tuple-like protocol used by the C++ standard, particularly those governing structured bindings. These mechanisms do not introduce tuple-like behavior but instead validate user-defined or library-defined types that claim to conform to the protocol.

Tuple-Like Validation

The jh::concepts::tuple_like concept performs structural verification of tuple-like semantics. A type is considered tuple-like when the following conditions are satisfied:

  • A valid std::tuple_size<T> specialization exists.
  • ADL get<I>(t) is available through argument-dependent lookup for each valid index.
  • Each element type exposed by get forms a valid std::common_reference_t with the corresponding std::tuple_element<I, T>::type.

These rules correspond to the semantics used by structured bindings. They also allow proxy-based tuple-like types whose element references differ from the declared tuple_element types as long as their common reference relationships represent compatible semantics.

Strict Pair-Like Matching

The jh::concepts::pair_like_for concept is a derived, more restrictive form of tuple-like validation. Inspired by proposals in Clang regarding the use of binary tuple-like structures as inputs to associative container interfaces, it verifies that:

  • The candidate type is tuple-like and has arity two.
  • The first and second element types match the expected key and value types exactly, after removal of cv-ref qualifiers.

Unlike tuple_like, which only checks semantic compatibility, pair_like_for enforces strict equality of canonical element types. This ensures the type behaves as a pair-like entity in a strong and predictable manner.

Version
1.3.x
Date
2025