C ++ 14 using an alias for is_same :: value

This question uses is_same<uint64_t, decltype(val)>::value .

I expected there would be C ++ 14 using alias : is_same_v , similar to the helper types: conditional_t , enable_if_t and tuple_element_t , which I use in my answer . Because the only thing I've ever used for any of these functions is to get type . Therefore, the *_t helper makes sense.

Which brings me to my question, why not using alias is_same_v in C ++ 14? The only thing I use is_same for is value . Perhaps using is_same usually does not apply to template declarations?

+7
c ++ templates c ++ 14 c ++ 17 helper
source share
3 answers

is_same_v (and other traits of type *_v ) were proposed by N3854 . They did not make it into C ++ 14, but they are in Fundamentals of the fundamental library> .

One of the problems was the potential coincidence with the โ€œConceptโ€ proposal, which could offer a better alternative for type traits (and many other current programming meta-methods). An outdated but clearer explanation of the concepts can be found here .

+6
source share

Introduction

The main reason for introducing std::enable_if_t<cond, T> as a shorter form of std::enable_if<cond, T>::type is not to shave only a 4-character number.

Since std::enable_if and other features of this type are mainly used in dependent contexts, it is rather painful to write (A) when (B) would be enough:

<sub> Example sub>

 template<class T, class = typename std::enable_if<cond, T>::type> // (A) struct A; 

 template<class T, class = std::enable_if_t<cond, T>> // (B) struct A; 


Dependent Names

We need a type name before std::enable_if , because ::type is a dependent name, and without it, the standard says that the expression should be parsed as if ::type were actually a value.

std::is_same<T, U>::value is really a value, so there is no need to use typename; which in turn means that we effectively shave just 4 characters. nothing more.


additional literature

  • fooobar.com/questions/11848 / ...


So why is there no variable template for std :: is_same?

Just because there is no such great need, therefore, no one has suggested adding in time; since most of them are satisfied with the following alternatives:

 std::is_same<T, U> {} == std::is_same<T, U>::value std::is_same<T, U> () == std::is_same<T, U>::value 


additional literature

There is a proposal written by Stefan T. Lavavi to add template variables for suitable types.

+8
source share

I would say that the main reason for introducing _t helpers was to get rid of having to place typename everywhere. When you use a type type, such as conditional or tuple_element , in the context of a template, so that it depends on the template parameter (which is a very common use case), you must prefix the construction with std::conditional<X, Y, Z>::type with using typename . You do not need to do this with the help of _t helpers, because they are not nested in what depends on the template parameters.

You do not need to add such a prefix for std::is_same<X, Y>::value , because memeber value not a type.

Secondly, how would you introduce a helper type for a value? The best you can do is a variable template. Since it will only save ::value , instead of typename /*...*/ ::type , this would probably be considered unnecessary.

+4
source share

All Articles