3.2. RareTs%3A ExtendedTypeSupport - TheNitesWhoSay/RareCpp GitHub Wiki

Indexing

  • forIndex(i, is, f) - Within a given index sequence, converts a runtime index to a constexpr index
  • forIndex(i, f) - For a given maximum, converts a runtime index (i where i >= 0 and i < Max) to a constexpr index
  • forIndexes(is, f) - For a given index sequence, iteratively calls the given function with each constexpr index
  • forIndexes(f) - For a given maximum, iteratively calls the given function with constexpr indexes from 0 to Max-1
  • packIndexes(is, f) - For a given index sequence, calls the given function once passing a pack of constexpr indexes in the sequence
  • packIndexes(f) - For a given maximum, calls the given function once passing a pack of constexpr indexes including 0 through Max-1

Misc Type Support

  • is_assignable - Bool trait checking whether the assignment operation "L = R" is valid
  • is_non_null - Bool trait checking whether the given type T is not (of any CV-qualification) of type std::nullptr_t
  • is_pointable - Bool trait checking whether the given type T is a pointer (either a regular C++ pointer or meets the criteria for smart pointers)
  • is_static_cast_assignable - Bool trait whether the assignment operation "L = static_cast<L>(R)" is valid
  • promote_char - Type trait returning int if the given type is a character, else returns the given type unchanged
  • remove_member_pointer - Type trait returning the underlying type of the member which a member pointer refers to
  • remove_pointer - Type trait removing pointers from the given type T
  • remove_cvref - Type trait which removes references and CV-qualifications from the given type T if present; prefer std::remove_cvref_t if in C++20
  • replace_void - Type trait returning the second type argument if T is void, else T
  • type_id - Type trait which defines a typedef "type" same as the given type T, prefer std::type_identity if in C++20
  • type_tag - A structure with no contents which is parameterized with a single type T

Op Detection & SFINAE

  • as_index - Alias for std::integral_constant<size_t, I>, has a type_list for SFINAE purposes, much like std::void_t
  • enable_if_constexpr - Alias for void, used to check whether a given variable is valid as an argument to a template/whether a variable is constexpr in some SFINAE context
  • first_of_t - Alias for the first given type, T, has a type_list after the first type for SFINAE purposes, much like std::void_t
  • op_exists - Bool trait which checks whether the given operation is valid for the given arguments

Container Support

  • element_type - Type trait that gets the type of a container (T::value_type) or array, if the type is a pair any consts on the pair members are removed
  • has_begin_end - Bool trait checking whether t.begin() and t.end() are valid operations on the given type T
  • has_clear - Bool trait checking whether t.clear() is a valid operation on the given type T
  • has_insert - Bool trait checking whether t.insert(e) is a valid operation on the given type T for the given element type E
  • has_insert_after - Bool trait checking whether t.insert_after(t.before_begin(), e) is a valid operation on the given type T for the given element type E (roughly means this is a forward_list or very similar container)
  • has_push - Bool trait checking whether t.push(e) is a valid operation on the given type T for the given element type E
  • has_push_back - Bool trait checking whether t.push_back(e) is a valid operation on the given type T for the given element type E
  • is_adaptor - Bool trait checking whether the given type T is an adaptor (has a ::container_type typedef)
  • is_iterable - Bool trait checking whether a given type T is iterable
  • is_map - Bool trait checking whether a given type T is a map (has a ::mapped_type typedef)
  • is_static_array - Bool trait checking whether a given type T is a fixed-size array (regular C++ array or std::array)
  • static_array_size - Index trait that retrieves the size of a fixed-size array (regular C++ array or std::array)
  • append - Appends the given element to the given container
  • baseContainer - Returns the container backing the given adaptor
  • clear - Clears the given container
  • isEmpty - Returns whether the given container is empty

Type List & Tuples

  • is_pair - Bool trait checking whether the given type is a std::pair
  • is_tuple - Bool trait checking whether the given type is a std::tuple
  • index_of - Index trait that gets the index of the given type
  • pair_lhs - Type trait that gets the first/left-hand type in a given pair
  • remove_index - Index trait that removes the given index from an index sequence
  • tuple_type_mask - Alias for type_mask applied specifically to a tuple type
  • remove_pair_const - Type trait that returns a pair type without const-qualifiers on the pair elements
  • type_list - Wrapper for various type-list parameterized traits
  • type_mask - Structure which defines the filtered index sequence type "indexes" that represents the indexes of types in the given type list that matched the given predicate
  • Masked - Wrapper for various tuple operations using an index filter
  • NttpTuple - A tuple where all of the elements are the given non-type template parameters.
  • referenceTuple - Returns a tuple whose only elements are references to the elements of the given source tuple which matched the given predicate

Specializations

  • is_specialization - Bool trait checking whether the given type is a specialization of the given template (does not work for NTTP-parameterized types)
  • is_specialized - Bool trait checking whether the given type extends the "Unspecialized" structure
  • Unspecialized - Structure which primary templates extend in order to be able to easily check whether a structure is a specialization using "is_specialized"

Strings & Type Rendering

  • StringIndexMap - Compile-time mapping of indexes to strings
  • TypeName - Compile-time null-terminated string representation of a type
  • fnv1aHash - Constexpr string-view hash function
  • toStr - Returns a string-view for the given type T

Indexing

forIndex(i, is, f)

  • i: The runtime index you wish to turn into a constexpr index
  • is: The index sequence that i can be within
  • f: The lambda/function/callable to be called with a constexpr index (std::integral_constant<size_t>)

Turns a single runtime index "i", into a constexpr index and passes it to "f"; only works for values within the index sequence.


forIndex(i, f)

  • <Max>: The maximum value that "i" can have
  • i: The runtime index you wish to turn into a constexpr index
  • f: The lambda/function/callable to be called with a constexpr index (std::integral_constant<size_t>)

Turns a single runtime index into a constexpr index and passes it to "f"; only works well for small ranges [0, Max).


forIndexes(is, f)

  • <Max>: The upper bound for the indexes you wish to loop
  • is: The index_sequence containing the indexes you want to have "f" called with
  • f: The lambda/function/callable to be called with each constexpr index (std::integral_constant<size_t>)

Calls "f" once for every index in index_sequence is, with the given iterations constexpr index (std::integral_constant<size_t>) as the argument.


forIndexes(f)

  • <Max>: The upper bound for the indexes you wish to loop
  • f: The lambda/function/callable to be called with each constexpr index (std::integral_constant<size_t>)

Calls "f" once for every index in [0, Max), with the given iterations constexpr index (std::integral_constant<size_t>) as the argument.


packIndexes(is, f)

  • is: The index sequence you wish to expand to a pack of indexes
  • f: The lambda/function/callable to be called with a pack of constexpr indexes (std::integral_constant<size_t>'s)

Calls "f" once with an argument list representing every index in is.


packIndexes(f)

  • <Max>: The upper bound for the indexes you wish to pack
  • f: The lambda/function/callable to be called witth a pack of constexpr indexes (std::integral_constant<size_t>'s)

Calls "f" once with an argument list representing every index in the range [0, Max)


Misc Type Support

is_assignable<L, R>

  • <L>: The left-hand/destination type
  • <R>: The right-hand/source type
  • value: bool indicating whether the assignment operation "L = R" is valid

Checks whether a value of type R is assignable to a value of type L


is_non_null<T>

  • <T>: The type to check
  • value: bool indicating whether the type T is not std::nullptr_t

Checks whether the type T is not std::nullptr_t


is_pointable<T>

  • <T> The type to check
  • value: bool indicating whether the type T is a pointer or meets the criteria for a std:: smart pointer

Checks whether the type T is a pointer or meets the criteria for a std:: smart pointer


is_static_cast_assignable<L, R>

  • <L>: The left-hand/destination type
  • <R>: The right-hand/source type
  • value: bool indicating whether the assignment operation "L = static_cast<L>(R)" is valid

Checks whether a value of type R static casted to L is assignable to a value of type L


promote_char<T>

  • <T>: The type to potentially be promoted
  • type: int if "T" was a character type, "T" otherwise

Promotes "T" to an integer if "T" is a character type, or leaves it unchanged otherwise.


remove_member_pointer<T>

  • <T>: The member pointer to get the underlying member type of
  • type: The underlying member type if "T" is a member pointer, "T" unchanged otherwise.

Gets the type of the underlying member for the given member pointer, "T".


remove_pointer<T>

  • <T>: The type to be dereferenced
  • type: "T" dereferenced if "T" is a pointer or meets the qualifications for a standard smart pointer, just "T" otherwise

Gets the type resulting when "T" is dereferenced if T is a pointer or meets the qualifications for a standard smart pointer, or "T" unchanged otherwise.


remove_cvref<T>

  • <T>: The type from which to remove const-volatile and reference qualifications
  • type: The type resulting from std::remove_cv_t<std::remove_reference_t<T>>

A simple trait for remove const-volatile and reference from a type, same as C++20's std::remove_cvref


replace_void<T, TypeIfVoid>

  • <T>: The type to be preserved if non-void or replaced if void
  • <TypeIfVoid>: The type to replace "T" with if "T" is void
  • type: "T" unchanged if "T" is not void, "TypeIfVoid" otherwise

Type trait replacing "T" with "TypeIfVoid" if "T" is void, or leaving "T" unchanged otherwise.


type_id<T>

  • <T>: The type to be returned unchanged
  • type: "T" unchanged

Wraps a type T, by publicly inheriting from type_id a structure obtains a member "type" of type "T", same as std::type_identity in C++20.


type_tag<T>

  • <T>: The type this structure is parameterized with

An empty structure parameterized with type "T", often useful for ADL.


Op Detection & SFINAE

as_index<I, Ts...>

  • <I>: The index this alias will resolve to
  • <Ts...>: An (unused) type list which is used to check whether statements are valid

A type alias resolving to std::integral_constant<size_t, I> assuming all types in "Ts..." are valid. Used much the same as std::void_t except it resolves to an integral_constant/"index" rather than void.


enable_if_constexpr<T, t>

  • <T>: The type of the value "t" to be checked
  • <t>: The value to check for availability in constexpr contexts

A void type-alias used to check whether some value "t" of type "T" is available in a constexpr context.


first_of_t<T, Ts...>

  • <T>: The first type which this type alias will resolve to
  • <Ts...>: An (unused) type list which is used to check whether statements are valid

A type alias that resolves to "T" assuming all types in "Ts..." are valid. Used much the same as std::void_t except it resolves to the first type it is given rather than void.


op_exists<Op, Args...>

Checks whether the given operation "Op" is valid given "Args...", this abstraction greatly simplifies the writing of traits that may otherwise require SFINAE to write.


Container Support

element_type<T>

  • <T>: The container or array from which to get the type of the elements
  • type: The type of the elements within the container or array

Type trait which gets the type of values within the container or array.


has_begin_end<T>

  • <T>: The type upon which to check whether .begin() and .end() are valid operations
  • value: bool indicating whether .begin() and .end() are valid operations upon an instance of "T"

Bool trait checking whether .begin() and .end() are valid operations on an instance of type "T"


has_clear<T>

  • <T>: The type upon which to check whether .clear() is a valid operation
  • value: bool indicating whether .clear() is a valid operation on an instance of type "T"

Bool trait checking whether .clear() is a valid operation on an instance of type "T"


has_insert<T, E>

  • <T>: The type upon which to check whether .insert(E) is a valid operation
  • <E>: The type of the inserted element
  • value: bool indicating whether .insert(E) is a valid operation on an instance of type "T"

Bool trait checking whether .insert(E) is a valid operation on an instance of type "T"


has_insert_after<T, E>

  • <T>: The type upon which to check whether t.insert_after(t.before_begin(), E) is a valid operation
  • <E>: The type of the inserted element
  • value: bool indicating whether t.insert_after(t.before_begin(), E) is a valid operation

Bool trait checking whether t.insert_after(t.before_begin(), E) is a valid operation (only expected to be valid on std::forward_list or a container mimicing forward_list).


has_push<T, E>

  • <T>: The type upon which to check whether .push(E) is a valid operation
  • <E>: The type of the inserted element
  • value: bool indicating whether t.push(E) is a valid operation

Bool trait checking whether t.push(E) is a valid operation


has_push_back<T, E>

  • <T>: The type upon which to check whether .push_back(E) is a valid operation
  • <E>: The type of the inserted element
  • value: bool indicating whether t.push_back(E) is a valid operation

Bool trait checking whether t.push_back(E) is a valid operation


is_adaptor<T>

  • <T>: The type to check
  • value: bool indicating whether T is an adaptor

Bool trait checking whether T is a container adaptor


is_iterable<T>

  • <T>: The type to check whether it's some kind of container or array
  • value: bool indicating whether T is some kind of container or array that can in some way be iterated

Bool trait checking whether T is a container or array that can in some way be iterated.


is_map<T>

  • <T>: The type to check
  • value: bool indicating whether "T" is some kind of map container

Bool trait checking whether T is some kind of map container.


is_static_array<T>

  • <T>: The type to check
  • value: bool indicating whether "T" is a regular C++ array or a std::array

Bool trait checking whether "T" is a regular C++ array or std::array


static_array_size<T>

  • <T>: The type to get the array size of
  • value: The size of the array "T"

Index trait getting the size of the static array "T"


append(container, element)

  • container: The container to append "element" to
  • element: The element to append to "container"

Appends "element" to "container", works for all standard library containers.


baseContainer(adaptor)

  • adaptor: The adaptor to get the base container for

Returns a const reference to the container backing this adaptor.


clear(iterable)

  • iterable: The container to be cleared

Removes all elements from the given container.


isEmpty(iterable)

  • iterable: The container to check for emptiness

Returns a bool indicating whether the given container was empty


Type List & Tuple

is_pair<T>

  • <T>: The type to check
  • value: bool indicating whether "T" is a std::pair

Bool trait checking whether the given type "T" is a pair


is_tuple<T>

  • <T>: The type to check
  • value: bool indicating whether "T" is a std::tuple

Bool trait checking whether the given type "T" is a tuple


index_of<T, L>

  • <T>: The type to find
  • <L>: The tuple or type list in which to find the index of "T"

Index trait getting the index of "T" in the tuple or type list "L"


pair_lhs<T>

  • <T>: The pair to get the left-hand type from
  • type: The left-hand type in the pair "T"

Type trait getting the left-hand type in the given pair.


remove_index<Remove, Is...>

  • <Remove>: The index to remove from the index list "Is..."
  • <Is...>: The index list from which to remove an index

Removes the index "Remove" from the index list "Is...", this operation is only valid if there's exactly one index in "Is..." with value "Remove"


tuple_type_mask<Predicate, T>

  • <Predicate>: The predicate by which to filter the given tuple
  • <T>: The tuple to filter
  • indexes: The indexes of the tuple "T" for which "Predicate" evaluated to true given the element type at index.

Forms an index sequence of all indexes of the tuple "T" for which "Predicate" evaluated to true given the element type at index.


remove_pair_const<T>

  • <T>: The pair from which consts are to be removed
  • type: The type of the pair without consts on the left/right-hand types

Type trait removing consts from the types within a pair.


type_list<Ts...>

  • <Ts...>: The types to perform one of the following operations on
  • has<T>: Bool trait checking whether the type list "Ts..." contains the type "T"
  • has_v<T>: Bool indicating whether the type list "Ts..." contains the type "T"
  • has_specialization<Of>: Bool trait checking whether the type list "Ts..." contains a specialization of template "Of"
  • has_specialization_v<Of>: Bool indicating whether the type list "Ts..." contains a specialization of template "Of"
  • get_specialization<Of>: Type trait getting the first specialization of "Of" in type list "Ts..."
  • get_specialization_t<Of>: Type alias getting the first specialization of "Of" in type list "Ts..."

type_list is an object wrapping some list of types "Ts..." and providing the above members


type_mask<Predicate, Ts...>

  • <Predicate>: The predicate to apply to a type list
  • <Ts...>: The types to get a mask for
  • indexes: An index sequence all the indexes of types in "Ts..." for which "Predicate" evaluates to true

Structure for getting the indexes of types in a type list "Ts..." which match "Predicate".


Masked<Is...>

  • <Is...>: The index sequence mask to apply to one of the operations below
  • indexOf<T, Tuple>: Index of type "T" in "Tuple" at one of the indexes given in "Is..."
  • indexOfSpecialization<Of, Tuple>: Index of specialization of "Of" in "Tuple" at one of the indexes given in "Is..."
  • get<T>(tuple): Gets the element of type "T" in "tuple" at one of the indexes in "Is..."
  • get<Of>(tuple): Gets the element specializing template "Of" in "tuple" at one of the indexes in "Is..."
  • forEach<T>(tuple, f): Calls "f" for every element of type "T" in "tuple" in indexes "Is..."
  • forEach<Of>(tuple, f): Calls "f" for every element specializing template "Of" in "tuple" in indexes "Is..."

Masked performs one of the above tuple operations only along the given indexes "Is..."


NttpTuple<auto...>

  • <auto...>: The values of the elements of the tuple, as non-type template parameters
  • element_type<I>: The type of the Ith element of the tuple (the Ith non-type template parameter)
  • get<I>(): gets the value of the Ith element of the tuple (the Ith non-type template parameter)

NttpTuple is a tuple where all of the elements are all of the given non-type template parameters.


referenceTuple<Predicate>(tuple)

  • <Predicate>: The predicate/filter to apply to the tuple
  • tuple: The source tuple

Returns a tuple containing references to the elements in the source "tuple" for which the "Predicate" evaluated to true.


Specializations

is_specialization<T, Of>

  • <T>: Type to check
  • <Of>: Template to check whether "T" is a specialization of
  • value: bool indicating whether "T" is a specialization of "Of"

Bool trait checking whether "T" is a specialization of template "Of"


is_specialized<T>

  • <T>: Type to check
  • value: bool indicating whether "T" inherits from the helper struct "Unspecialized"

Bool trait checking whether "T" inherits from the helper struct "Unspecialized", by having the primary template inherit from "Unspecialized" and specializations not, this can check whether some type "T" is a specialization of some primary template that inherits from "Unspecialized".


Unspecialized<T>

Helper structure for "is_specialized", primary templates intended to be used with "is_specialized" should inherit from Unspecialized.


Strings & Type Rendering

StringIndexMap<s...>

  • <s...>: The strings to form an index-mapping for
  • total: The total number of strings in the map (sizeof...(s))
  • indexOf(string): Gets the index of "string" in "s..."

StringIndexMap is a constexpr hash map mapping strings "s..." to the indexes at which they were passed in.


TypeName<T>

  • <T>: The type this is a string representation of
  • length: The length of the string-representation of "T"
  • value: A character array containing a null-terminated string-representation of "T"

A constexpr-friendly null-terminated string representation for a type "T"


fnv1aHash(str)

  • str: The string to hash

Returns the hash value of str, this is a constexpr-friendly string hash function, used by StringIndexMap.


toStr<T>

  • <T>: The type to convert to a string representation

Returns a string-representation of the given type "T"


⚠️ **GitHub.com Fallback** ⚠️