Document #: | P2008R0 |
Date: | 2020-01-10 |
Project: | Programming Language C++ Evolution Working Group Incubator |
Reply-to: |
Mateusz Pusz (Epam Systems) <mateusz.pusz@gmail.com> |
In C++ we can easily form a template that will take a class template as its parameter. With C++14 we got variable templates. They proved to be useful in many domains but still are not first-class citizens in C++. For example, they cannot be passed as a template parameter to a template. This document proposes adding such a feature.
To check if the current type is an instantiation of a class template we can write the following type trait:
template<typename T>
struct is_ratio : std::false_type {};
template<intmax_t Num, intmax_t Den>
struct is_ratio<std::ratio<Num, Den>> : std::true_type {};
A family of such type traits can be passed to a concept:
and then be used to constrain a template:
After C++14 we learnt that variable templates are less to type (no need for a _v
helper) and are often faster to compile. The above is_ratio
type trait can be rewritten as:
template<typename T>
inline constexpr bool is_ratio = false;
template<intmax_t Num, intmax_t Den>
inline constexpr bool is_ratio<ratio<Num, Den>> = true;
However, contrary to a class template, the above variable template cannot be passed to a template at all. The syntax:
is not a valid C++ as of today.
Extend C++ template syntax with a variable template parameter kind.
Special thanks and recognition goes to Epam Systems for supporting my membership in the ISO C++ Committee and the production of this proposal.