10

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

I expected there to be a 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 ever use any of those functions for is to get the type. So the *_t helper just makes sense.

Which brings me to my question, why is there no using alias is_same_v in C++14? The only thing I use is_same for is it's value. Perhaps the usage of is_same is not typically for template declarations?

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • 1
    `is_same::value` is not a type. – Wintermute Mar 09 '15 at 12:12
  • @Wintermute: There is still template variable. – Jarod42 Mar 09 '15 at 12:14
  • Yes, but how would you have a helper type that's not a type? Also, you can usually just use `std::is_same()` because of the `operator bool`. – Wintermute Mar 09 '15 at 12:14
  • @Wintermute Right, I got that, I'm not asking for `is_size_` **`t`** I'm asking for `is_size_` **`v`**. Similar to the way the helper type `enable_if_t` is defined: http://en.cppreference.com/w/cpp/types/enable_if#Helper_types – Jonathan Mee Mar 09 '15 at 12:15
  • `std::enable_if_t` is a type, you'll notice. No type, no type alias. You could, I suppose, build a `constexpr` function to be called as `std::is_same_v()`. – Wintermute Mar 09 '15 at 12:18
  • I think you mean "using alias" not "helper type", as `is_same_v<...>` wouldn't be a type, as that would be pointless. :) – Yakk - Adam Nevraumont Mar 11 '15 at 19:12
  • @Yakk I've edited cause you're totally right. Looking at all the comments that's what confused everyone, I was just using the wrong terminology. I wish I'd have recognized this 2 days ago! – Jonathan Mee Mar 11 '15 at 19:21

3 Answers3

8

Introduction

The primary reason for introducing std::enable_if_t<cond, T> as a shorter form ofstd::enable_if<cond, T>::type is not to shave of a mere count of 4 characters.

Since std::enable_if, and other type-traits of its kind, is mostly used in dependent contexts, it is quite painful having to write (A) when (B) would suffice:

Example

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 typename prior to std::enable_if because ::type is a dependent-name, and without it the Standard says that the expression shall be parsed as if ::type is actually a value.

std::is_same<T, U>::value is really a value, so there's no need for the use of typename; which in turns mean that we are effectively shaving of a mere count of 4 characters.. nothing more.


Further Reading



So, why isn't there a variable-template for std::is_same?

Simply because there isn't that big of a need, so no one did propose the addition in time; as most are satisfied with the below alternatives:

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

Further Reading

There is a proposal, written by Stephan T. Lavavej, to add variable-templates for the suitable type-traits.

Community
  • 1
  • 1
Filip Roséen - refp
  • 62,493
  • 20
  • 150
  • 196
7

is_same_v (and other *_v type traits) have been proposed by N3854. They didn't make into C++14 but they are in the Library Fundamentals TS.

One of the concerns was a potential overlap with the Concepts Proposal which might offer a better alternative for type traits (and many other current meta-programming techniques). An outdated but clearer explanation of Concepts can be found here.

Cassio Neri
  • 19,583
  • 7
  • 46
  • 68
4

I'd say that the primary reason for introducing the _t helpers was to get rid of the need for putting typename everywhere. When you use a type trait such as conditional or tuple_element in the context of a template so that it depends on a template parameter (which is a very common use case), you have to prefix the construct std::conditional<X, Y, Z>::type with typename. You don't have to do that with the _t helpers, because they aren't nested in something which depends on template parameters.

You don't have to add any such prefix for std::is_same<X, Y>::value, because the memeber value is not a type.

Second, how would you introduce a helper type for a value, anyway? The best you could do is a variable template. Since it would only save typing ::value, instead of typename /*...*/ ::type, it was probably deemed unnecessary.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455