-
Notifications
You must be signed in to change notification settings - Fork 0
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.
Common.hpp
contains templated tools for type navigation, all defined in the Langulus
namespace. Here's a short list:
-
Deref<T>
- acts asstd::remove_reference_t
. Removes the topmost reference of a typeT
. -
Deptr<T>
- acts asstd::remove_pointer_t
. Removes the topmost pointer of a typeT
. -
Decvq<T>
- acts asstd::remove_cv_t
. Removes the topmostconst
andvolatile
qualifiers of a typeT
. -
Deext<T>
- acts asstd::remove_extent_t
. Removes the topmost bounded array extent ([x]) ofT
. -
Decay<T>
- strip a typename to its origin type, removing all nested qualifiers, pointers, extents, and references. -
Fake<T>()
- same asstd::declval
, but more intuitively named. Useful to create a 'fake' reference to typeT
in unevaluated contexts. -
Conditional<CONDITION, TRUE_T, FALSE_T>
- acts asstd::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). Seestd::extent_v
. -
DecvqAll<T>
- strip all qualifiers on all levels of indirection, while preserving the indirections themselves:const volatile void * const * const
becomes justvoid**
. -
DecvqCast(T&) -> DecvqAll<T>&
- function that performs aDecvqAll
on a reference. -
DecvqCast(CT::Sparse auto&) -> DecvqAll<T>
- function that performs aDecvqAll
on a pointer.