在 C++17 中弃用 std::is_literal_type

Deprecated std::is_literal_type in C++17

根据cppreference, the trait std::is_literal_type is deprecated in C++17. The question is why and what is the preferred replacement for the future to check whether a type is a literal type

As stated in P0174:

The is_literal type trait offers negligible value to generic code, as what is really needed is the ability to know that a specific construction would produce constant initialization. The core term of a literal type having at least one constexpr constructor is too weak to be used meaningfully.

基本上,它的意思是没有任何代码可以用 is_literal_type_v 保护,并且足以确保您的代码实际上是 constexpr。这还不够好:

template<typename T>
std::enable_if_t<std::is_literal_type_v<T>, void> SomeFunc()
{
  constexpr T t{};
}

不能保证这是合法的。即使你用 is_default_constructible<T> 保护它,也不意味着它是 constexpr 默认可构造的。

您需要的是 is_constexpr_constructible 特质。目前尚不存在。

However, the (already implemented) trait does no harm, and allows compile-time introspection for which core-language type-categories a given template parameter might satisfy. Until the Core Working Group retire the notion of a literal type, the corresponding library trait should be preserved.

The next step towards removal (after deprecation) would be to write a paper proposing to remove the term from the core language while deprecating/removing the type trait.

所以计划是最终摆脱 "literal types" 的整个定义,用更细粒度的东西取而代之。