DEENESPTFRRUARHIKOZHNL

struct std::hash< ara::core::Variant<

[SWS_CORE_01668]

template <typename... Types> struct hash< ara::core::Variant< Types...            > > {...};

Specialization of std::hash (hashing function for ara::core::Variant<Types...>)

Header-Datei#include "ara/core/variant.h"
Forwarding-Header#include "ara/core/core_fwd.h"
Scopenamespace std

User GuideAI-GENERATED

Enables the use of type-safe variants as keys in unordered standard containers via hash values.
AI-GENERATED unreviewed Model: gemini-3.8-flash-medium · Effort: medium · Temperature: not controllable · Prompt: 9c32f84b6c4d Provenance Open questions (3)
translated from the German guidegemini-3.8-flash-low · 2026-10-13 · German original

Enables the use of type-safe variants as keys in unordered standard containers via hash values.

The template specialization provides a hash function for type-safe union types in order to enable their seamless use in associative hash containers. It acts as a standard-compliant function adapter that reads the state of an instance and converts it into an unambiguous hash value. A prerequisite for its use is the hashability of all alternative types contained in the type list. If the content remains unchanged, the evaluation operates concurrency-safely and free of side effects.

Context

The template specialization std::hash<ara::core::Variant<Types...>> extends the standardized hashing framework of the C++ standard library for the AUTOSAR Adaptive core type ara::core::Variant. Within module core, ara::core::Variant acts as a type-safe, memory-optimized union that can hold exactly one instance from a defined list of alternative types at any given time. In order to use such variant data structures as keys in associative hash containers such as std::unordered_map or std::unordered_set, a corresponding specialization of std::hash is mandatory.

The specialization thus mediates seamlessly between the functional requirements of the AUTOSAR Adaptive Platform and the generic algorithms and containers of the standard library. It allows software components to efficiently index, forward, or cache polymorphic data structures without dynamic type casts or user-defined wrapper functors.

Lifecycle

The lifecycle of the hash specialization is extremely lightweight and modeled after the conventions of C++ standard functors. The class has no persistent state variables or referenced resources. Creation therefore occurs trivially via the implicit default constructor, typically directly at the call site on the stack or as a temporary object within container operations.

Since the specialization is stateless, the object is likely to be freely copyable and movable. Question Q-2cffd290e7f00968 (open) Destruction does not require any explicit release of resources or destructor logic. With regard to concurrency, invoking the functor is thread-safe as long as the ara::core::Variant instance to be hashed is not modified concurrently by another thread of execution. Multiple threads can use the same instance of the hash functor or parallel instances simultaneously without requiring external synchronization.

Usage Path

Typical use occurs indirectly via associative containers of the standard library or directly via explicit execution of the call operator operator(). A use case begins with the provision of an initialized instance of ara::core::Variant that holds a concrete alternative value.

When the hash function is called, the current state of the variant is queried. The calculation is likely to proceed in two conceptual sub-steps: First, the discriminator or index of the currently held type is determined. Question (open) Next, the concrete value contained in the variant is passed to the matching std::hash specialization of this type. Finally, the type index and the value hash are combined with each other to produce the resulting hash value of type std::size_t. This ensures that variants with an identical alternative value, but different type identity, reliably receive different hash values.

Error Scenarios

The robustness of the hash calculation largely depends on the template parameters and the state of the passed variant. A fundamental precondition is that a functional specialization of std::hash exists for all types declared in parameter pack Types.... If such a definition is missing for even a single type, compilation fails as soon as the hash functor is instantiated.

At runtime, the method requires that the passed variant is not invalid. If the underlying variant object has entered an exceptional valueless-by-exception state due to a previously failed assignment, the behavior of the hash calculation is presumably undefined or results in a runtime exception. Question Q-d1e0b4858aa9dc16 (open) In addition, the hash calculation of the individual alternative types is expected not to throw exceptions in order to meet the requirements for standard hash functions.

This text was generated by an AI based on the AUTOSAR specification R25-11.

UML class diagram

Hohle Dreieckspfeile = Vererbung (zeigt zur Basisklasse); Klicken öffnet die jeweilige Klassenseite.

G nstd__hash__ara__core__Variant_545 «struct» hash< ara::core::Variant< n_core__Variant__Types__________________609 :core::Variant< Types...            > > nstd__hash__ara__core__Variant_545->n_core__Variant__Types__________________609