Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
type_info: Additional Information About Types

The type info sub-package provides additional information about a type beyond those provided by the type_traits package.

The public header is

Public export header for content from arene/base/type_info.

The Bazel target is

//:type_info

Additional Information About Types

The type_info sub-package provides facilities for obtaining additional information about a type.

Getting The Name Of A Type As A String

arene::base::type_name_v allows you to obtain an arene::base::inline_string which holds the name of that type. For example, arene::base::type_name_v<arene::base::result<int,my_error>> may yield an arene::base::inline_string holding "arene::base::result<int,my_error>".

This is similar to typeid(T).name(), except that typeid requires RTTI enabled, frequently yields a mangled name rather than a human-readable one, and cannot be used in constant expressions.

The precise form of the string is compiler dependent, especially with regard to types in anonymous namespaces, or integer types other than int (arene::base::type_name_v<unsigned short> might be "unsigned short" or "short unsigned int" depending on the compiler). However, these strings should be clearly identifiable to people, and yield a unique string for every type in a translation unit.

arene::base::type_name_v is declared constexpr, so the strings can be used in constant expressions.

Example usage:

template <typename T>
void print_with_type_name(T&& val) {
std::cout << arene::base::string_view{arene::base::type_name_v<T>} << " : " << val << "\n";
}

Here, print_with_type_name prints the name of the type of the argument, as well as the value.

template <typename A, typename B>
using ordered_pair =
using pair1 = ordered_pair<int, double>;
using pair2 = ordered_pair<double, int>;
static_assert(std::is_same<pair1, pair2>::value, "Types must be the same");

Here, ordered_pair uses the type name to order the template arguments to ensure that ordered_pair<A,B> is the same as ordered_pair<B,A>, regardless of where the types are used, and without imposing any requirements on A and B.