In Jody Hagins' 2020 talk at Cppcon [link], he gave the following example of a metafunction as follows:
template <typename T>
struct remove_volatile : TypeIdentity<T> {};
template <typename T>
struct remove_volatile<T volatile> : TypeIdentity<T> {};
template <typename T>
using remove_volatile_t = typename remove_volatile<T>::type;
He mentioned, not exactly but along the lines of, that we can treat a metafunction as a function that operates on types where in struct remove_volatile<T volatile> : TypeIdentity<T> {};
we can sort of think of T volatile
as the input to our metafunction remove_volatile
and the output we get is TypeIdentity<T>
. To invoke this metafunction, we can use, as an example remove_volatile_t<int volatile>
.
In the comment on this topic, @riking mentioned the following:
Rust type-level functions take the form of traits and associated types.
The trait is the "function", the type it's implemented on and
the generic parameters are the "inputs",
and the associated types are the "outputs".
I am inquisitive if there is a 1-1 correspondence between C++ metafunctions and Rust's traits and if so, what are the correspondences?