Jakob Riedle
Jakob Riedle

Reputation: 2018

Why do all type_traits Classes have to be called using 'typename' and '::type' Prefix/Suffix?

This is kind of an odd question although it is pretty obvious:

Why do all templates in <type_traits> have to be called with typenname and ::type prefix/suffix?

One reason would be, of course, that there was no templated typedef like the C++0x using thingy, that allows the following:

template<typename T>
using remove_ref = typename std::remove_reference<T>::type;

remove_ref<int&> foo = 4;

So this question is less about, why it currently is that way, but more about whether this behaviour is going to be simplified in future C++ standards?.

Similar improvement could come with traits like std::is_pointer<T>::value - I can already see templated constants at the horizon of C++14/17 that would allow uses like std::is_pointer<T>.

Note: To my knowledge, this simplification is not subject of any of the published items concerning the upcoming C++ standards. In this case, there is no real yes/no answer to this question and this thread could act as a pro/con list whether this is likely to be changed in any new versions of C++.

EDIT:

Thanks to @Drew_Dormann and @erenon that have correctly pointed out that there already have been _t versions added to all type trait templates that exactly do that.

However, it is still open, whether there are any signs that value traits like std::is_pointer will be part of a similar simplification besides the std::is_pointer<T>()-Version?

Upvotes: 0

Views: 535

Answers (2)

T.C.
T.C.

Reputation: 137310

However, it is still open, whether there are any signs that value traits like std::is_pointer will be part of a similar simplification besides the std::is_pointer<T>()-Version?

Yes. The first library fundamentals TS, currently in DTS ballot, adds numerous _v variable templates. For example:

template <class T> constexpr bool is_pointer_v
  = is_pointer<T>::value;

Like everything added in a TS, it's in the std::experimental namespace. They have been implemented in the trunk version of libstdc++ and libc++.

Upvotes: 4

erenon
erenon

Reputation: 19118

In c++14, there is remove_reference_t, which is exactly what you are looking for.

Upvotes: 5

Related Questions