为什么必须使用 'typename' 和 '::type' Prefix/Suffix 调用所有 type_traits 类?

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

这个问题有点奇怪,虽然很明显:

为什么 <type_traits> 中的所有模板都必须用 typenname::type prefix/suffix 调用?

当然,一个原因是没有像 C++0x using 这样的模板化类型定义,它允许以下操作:

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

remove_ref<int&> foo = 4;

所以这个问题不是关于为什么现在是这样,而是更多关于这种行为是否会在未来的 C++ 标准中得到简化?.

类似的改进可能会伴随 std::is_pointer<T>::value 等特征 - 我已经可以在 C++14/17 的 horizon 中看到模板化常量,这些常量允许使用 std::is_pointer<T>

注意: 据我所知,这种简化不是任何有关即将发布的 C++ 标准的已发布项目的主题。在这种情况下,这个问题没有真正的 yes/no 答案,并且这个线程可以充当 pro/con 列表是否可能在任何新版本的 C++ 中更改。

编辑:

感谢 @Drew_Dormann 和 @erenon 正确地指出,已经有 _t 版本添加到所有类型特征模板中,它们正是这样做的。

但是,它仍然是开放的,是否有任何迹象表明像 std::is_pointer 这样的价值特征将成为除了 std::is_pointer<T>()-Version 之外的类似简化的一部分?

在c++14中,有remove_reference_t,这正是你要找的

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?

是的。第一个 library fundamentals TS,目前在 DTS 投票中,添加了许多 _v 变量模板。例如:

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

就像在 TS 中添加的所有内容一样,它位于 std::experimental 命名空间中。它们已在 libstdc++ 和 libc++ 的主干版本中实现。