Skip to content

Type Navigation

Dimo Markov edited this page May 23, 2024 · 2 revisions

Type navigation combined with concepts are one of the most powerful meta programming tools out there. Look at this snippet:

if constexpr (CT::Dense<T>)
   return static_cast<const Deref<T>&>(*GetRaw()[idx]);
else
   return static_cast<const Deptr<Deref<T>>*>(GetRaw()[idx]);

This code decides what the return type will be, based on whether T is pointer (sparse) or not (dense) at compile-time. Deref<T> will remove any reference from T and attach a new constant one instead. On the other hand, if T ends up sparse, we will remove the reference (Deref<T>), and the pointer (Deptr<Deref<T>>), after which it will reattach a new constant pointer onto the resulting type. Needless to say, such patterns are used all over Langulus, and are extremely useful for generic programming.

Catalogue

Common.hpp contains templated tools for type navigation, all defined in the Langulus namespace. Here's a short list:

  • Deref<T> - acts as std::remove_reference_t. Removes the topmost reference of a type T.
  • Deptr<T> - acts as std::remove_pointer_t. Removes the topmost pointer of a type T.
  • Decvq<T> - acts as std::remove_cv_t. Removes the topmost const and volatile qualifiers of a type T.
  • Deext<T> - acts as std::remove_extent_t. Removes the topmost bounded array extent ([x]) of T.
  • Decay<T> - strip a typename to its origin type, removing all nested qualifiers, pointers, extents, and references.
  • Fake<T>() - same as std::declval, but more intuitively named. Useful to create a 'fake' reference to type T in unevaluated contexts.
  • Conditional<CONDITION, TRUE_T, FALSE_T> - acts as std::conditional_t - useful to select a type by a compile-time condition.
  • ExtentOf<T> - extracts the size of the topmost bounded array. Will always give 1 for non-array types (including unbounded array pointers). See std::extent_v.
  • DecvqAll<T> - strip all qualifiers on all levels of indirection, while preserving the indirections themselves: const volatile void * const * const becomes just void**.
  • DecvqCast(T&) -> DecvqAll<T>& - function that performs a DecvqAll on a reference.
  • DecvqCast(CT::Sparse auto&) -> DecvqAll<T> - function that performs a DecvqAll on a pointer.
Clone this wiki locally