C ++ 17中不推荐使用的std :: is_literal_type [英] Deprecated std::is_literal_type in C++17
问题描述
根据 cppreference ,特征 std :: is_literal_type
在C ++ 17中已弃用。问题是为什么和什么是首选替代品,以便将来检查类型是否为文字类型。
According to 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.
推荐答案
The
is_literal
类型特征为通用代码提供了微不足道的价值,因为真正需要的是能够知道特定构造会产生常量初始化的能力。具有至少一个constexpr构造函数的<文字>文字类型的核心术语太弱而无法有意义地使用。
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。这还不够好:
Basically, what it's saying is that there's no code you can guard with is_literal_type_v
and have that be sufficient to ensure that your code actually is constexpr. This isn't good enough:
template<typename T>
std::enable_if_t<std::is_literal_type_v<T>, void> SomeFunc()
{
constexpr T t{};
}
我们无法保证这是合法的。即使您使用 is_default_constructible< T>
保护它,这也不意味着它是 constexpr 默认可构造的。
There's no guarantee that this is legal. Even if you guard it with is_default_constructible<T>
that doesn't mean that it's constexpr default constructible.
您需要的是 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.
因此,该计划最终要摆脱文字类型的整个定义,用更细粒度的内容代替。
So the plan is to eventually get rid of the whole definition of "literal types", replacing it with something more fine-grained.
这篇关于C ++ 17中不推荐使用的std :: is_literal_type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!