Can type to value meta-function be used as variable alias in C++14?

612 views Asked by At

While looking at C++14 the meta-function alias proposals (TransformationTraits Redux, v2,N3655), I noticed that, not only type to type transformations (such as add_const), type to value meta-functions (such as is_void ) are also type aliased. (Which are not present in N3797).

Is there any advantage of aliasing type to value meta-functions? I think, it is possible to use them without those aliases, such as enable_if_t<is_void<T>::value,T> or enable_if_t<is_void<T>{}(),T> when the conversion operation is present. (I guess is_void<T>::type::value is same as is_void<T>::value)

If the type to value meta-functions need to be aliases, wont it be better to alias them as variable template ( I do not have C++14 compiler and never used variable template. So the syntax may be wrong)? e.g. alias is_void as

template <class T>
constexpr bool is_void_t = is_void<T>::value;

Instead of

template <class T>
using is_void_t = typename is_void<T>::type;

Then one can write enable_if_t<is_void_t<T>,T> without boost style enable_if,and composing expression will be easier (e.g. enable_if_t<(is_void_t<T> || is_integral_t<T>),T>

1

There are 1 answers

6
gx_ On BEST ANSWER

“Is there any advantage of aliasing type to value meta-functions?”

Quoting from the linked N3655, before the specifications of is_void_t and siblings (page 4):

4   Supplementary proposed wording

The following wording is provided in response to LWG’s request that aliases for ::type members be consistently provided for all the type traits, not only for those classified as TransformationTraits. Accordingly, this section provides the specifications needed in order to complete the set.

That explains why “type to value meta-functions (such as is_void ) are also type aliased” in the proposal: for consistency.

Moreover, it would be wrong to use the name “is_void_t” to alias is_void<T>::value. The “_t” suffix always denotes a type. For the value, maybe one could use a “_v” suffix. So we would then have both:

template <class T>
using is_void_t = typename is_void<T>::type;

template <class T>
constexpr bool is_void_v = is_void<T>::value;

That should compile in C++14, and then you could write things like enable_if_t<(is_void_v<T> || is_integral_v<T>),T>. But I feel that the value alias is “less needed” than the type one: it doesn't save as much typing, and, as you said, you can use the short is_void<T>{}() with the same effect (and for C++11, is_void<T>{} is often enough, thanks to its operator bool()).