简体   繁体   English

C ++ 17中不推荐使用std :: is_literal_type

[英]Deprecated std::is_literal_type in C++17

According to cppreference , the trait std::is_literal_type is deprecated in C++17. 根据cppreference ,特性std::is_literal_type在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 : 如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. is_literal类型特征为通用代码提供的值可忽略不计,因为真正需要的是能够知道特定构造将产生持续初始化。 The core term of a literal type having at least one constexpr constructor is too weak to be used meaningfully. 具有至少一个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. 基本上,它所说的是没有代码可以用is_literal_type_v来保护,并且这足以确保你的代码实际上是constexpr。 This isn't good enough: 这还不够好:

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

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_default_constructible<T>来保护它,也不意味着它是constexpr默认的可构造性。

What you would need is an is_constexpr_constructible trait. 你需要的是一个is_constexpr_constructible特征。 Which does not as of yet exist. 哪个尚未存在。

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. 所以计划最终要摆脱“文字类型”的整个定义,用更细粒度的东西取而代之。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换 C++17 的“if constexpr(std::is_literal_type<t> ::value)" 到 C++11 SFINAE 代码?</t> - How to convert C++17's "if constexpr(std::is_literal_type<T>::value)" to C++11 SFINAE code? C++17 中不推荐使用 `std::result_of` 的原因是什么? - What is the reason for `std::result_of` deprecated in C++17? libc ++的std :: is_literal_type如何工作? - How does libc++'s std::is_literal_type work? 使用 C++17 在编译时将 C 字符串文字转换为 std::integer_sequence - Turn C string literal into std::integer_sequence at compile-time with C++17 为什么在c ++ 17中不推荐使用std :: allocator的构造和销毁函数? - Why are are std::allocator's construct and destroy functions deprecated in c++17? 为什么`std :: reference_wrapper`在c ++ 17中被弃用,而在c ++ 20中被删除? - Why is `std::reference_wrapper` deprecated in c++17 and removed in c++20? C ++ 17中名称空间的属性“不建议使用” - Attribute “deprecated” to namespace in C++17 为什么std :: is_literal_type <std::String> ==错误,我该如何解决? - Why does std::is_literal_type<std::String> == false and how can I get around it? C ++ 17标准是否包含“std :: byte”? - Will the C++17 standard include “std::byte”? 在c ++ 17中使用std :: destroy_at()? - Usage of std::destroy_at() in c++17?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM