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
jh::pod::optional< T > Struct Template Referencefinal

POD-compatible optional wrapper. More...

#include <jh/pods/optional.h>

Public Types

using value_type = T
 Alias of contained type.

Public Member Functions

constexpr optional () noexcept=default
 Default constructor (empty state).
void store (const T &value) noexcept
 Store a value by copying raw memory.
void clear () noexcept
 Clear the stored value (set to empty).
T * get () noexcept
 Get mutable pointer to stored value.
const T * get () const noexcept
 Get const pointer to stored value.
bool has () const noexcept
 Whether a value is present.
bool empty () const noexcept
 Whether the optional is empty.
T & ref () noexcept
 Access stored value by reference.
const T & ref () const noexcept
 Access stored value by const reference.
value_or (T fallback) const noexcept
 Return stored value or fallback.
constexpr bool operator== (const optional &rhs) const noexcept
 Equality comparison with another optional.

Public Attributes

std::byte storage [sizeof(T)]
 Raw storage; flattens type ABI, never access directly.
bool has_value
 Presence flag (true = has value).

Detailed Description

template<cv_free_pod_like T>
struct jh::pod::optional< T >

POD-compatible optional wrapper.

Stores raw bytes for T and a boolean flag. Provides POD-level semantics similar to std::optional.

Equality Semantics:

  • If one has value and the other not → false
  • If both are empty → true (ignores raw storage)
  • If both have value → compare storage bytes (memcmp)

Usage Model:

Template Parameters
TElement type. Must satisfy pod_like<T> and be free of const/volatile qualifiers (i.e. satisfy cv_free_pod_like<T>). Using a cv-qualified type would make the optional itself non-POD due to loss of trivial assignment and standard layout guarantees.

Member Function Documentation

◆ get() [1/2]

template<cv_free_pod_like T>
const T * jh::pod::optional< T >::get ( ) const
inlinenodiscardnoexcept

Get const pointer to stored value.

Returns
Pointer to const T, must check .has() before use.

◆ get() [2/2]

template<cv_free_pod_like T>
T * jh::pod::optional< T >::get ( )
inlinenoexcept

Get mutable pointer to stored value.

Returns
Pointer to T, must check .has() before use.

◆ operator==()

template<cv_free_pod_like T>
bool jh::pod::optional< T >::operator== ( const optional< T > & rhs) const
inlineconstexprnoexcept

Equality comparison with another optional.

Semantics are aligned with std::optional:

  • If one has a value and the other does not → false
  • If both are empty → true
  • If both have a value → compare the underlying storage bytes
Parameters
rhsOther optional to compare with.
Returns
true if both optionals have the same state and (if present) identical raw byte content, false otherwise.
Note
This operator does not rely on = default, because the default comparison would also require raw storage equality when has_value == false. That would force meaningless zeroing of storage in .clear(). Instead, we define comparison explicitly: empty optionals are always equal regardless of storage content.
Raw comparison is performed with std::memcmp, ensuring POD-level semantics without invoking T::operator==.

◆ ref() [1/2]

template<cv_free_pod_like T>
const T & jh::pod::optional< T >::ref ( ) const
inlinenodiscardnoexcept

Access stored value by const reference.

Returns
Const reference to T. Undefined if .has() == false.

◆ ref() [2/2]

template<cv_free_pod_like T>
T & jh::pod::optional< T >::ref ( )
inlinenodiscardnoexcept

Access stored value by reference.

Returns
Reference to T. Undefined if .has() == false.

◆ store()

template<cv_free_pod_like T>
void jh::pod::optional< T >::store ( const T & value)
inlinenoexcept

Store a value by copying raw memory.

Parameters
valueSource value to copy.

◆ value_or()

template<cv_free_pod_like T>
T jh::pod::optional< T >::value_or ( T fallback) const
inlinenodiscardnoexcept

Return stored value or fallback.

Parameters
fallbackValue to return if empty.
Returns
Copy of stored or fallback value.

The documentation for this struct was generated from the following file: