C++ 20概念首次被支持 Visual Studio 2019版本16.3预览版2 . 这包括编译器和标准库支持。
首先,我们将通过/ std:c ++最新模式,一旦我们拥有了所有VisualStudio产品(编译器、库、智能感知、构建系统、调试器)上实现的所有C++ 20特性, 等 . )我们会通过新的/ std:c ++20模式。 智能感知支持目前不可用,我们的实现还没有包括Cologne ISO C++标准会议的最新变化。
什么是C++概念?
概念是谓词,用于表示泛型算法对其模板参数的期望。
概念允许您正式记录模板上的约束,并让编译器强制执行这些约束。作为奖励,您还可以利用这种强制,通过基于概念的重载来提高程序的编译时间。
网上有许多关于概念的有用资源。例如, isocpp公司 有许多关于概念的博客文章,其中包括 本贾尼·斯特劳斯特卢普 .
支持什么?
编译器支持包括 :
- P0734 R0:用纸,概念的C++扩展
- 还有帕特 c 由于措辞的变化 P1141R2:约束声明的另一种方法 ,但不包括缩写函数模板和带有类型约束的占位符类型说明符。
- P0857R0:“约束中的功能缺口”的措辞
- P1084R2:今天的退货类型要求不够
- P0717R1型 :概念的语义约束匹配
编译器支持不包括Cologne ISO C++标准会议的最新更改。
库支持包括: <概念>
示例
在这里 是一些 例子 s 概念如何帮助您编写更简洁的代码。它们的编译时间也更短。
#include <concepts> // This concept tests whether 'T::type' is a valid type template<typename T> concept has_type_member = requires { typename T::type; }; struct S1 {}; struct S2 { using type = int; }; static_assert(!has_type_member<S1>); static_assert(has_type_member<S2>); // Currently, MSVC doesn't support requires-expressions everywhere; they only work in concept definitions and in requires-clauses //template <class T> constexpr bool has_type_member_f(T) { return requires{ typename T::type; }; } template <class T> constexpr bool has_type_member_f(T) { return has_type_member<T>; } static_assert(!has_type_member_f(S1{})); static_assert(has_type_member_f(S2{})); // This concept tests whether 'T::value' is a valid expression which can be implicitly converted to bool // 'std::convertible_to' is a concept defined in <concepts> template<typename T> concept has_bool_value_member = requires { { T::value } -> std::convertible_to<bool>; }; struct S3 {}; struct S4 { static constexpr bool value = true; }; struct S5 { static constexpr S3 value{}; }; static_assert(!has_bool_value_member<S3>); static_assert(has_bool_value_member<S4>); static_assert(!has_bool_value_member<S5>); // The function is only a viable candidate if 'T::value' is a valid expression which can be implicitly converted to bool template<has_bool_value_member T> bool get_value() { return T::value; } // This concept tests whether 't + u' is a valid expression template<typename T, typename U> concept can_add = requires(T t, U u) { t + u; }; // The function is only a viable candidate if 't + u' is a valid expression template<typename T, typename U> requires can_add<T, U> auto add(T t, U u) { return t + u; }
那靶场呢?
我们也在研究 范围 . 它 提供用于处理 要素 而且有一个 紧的 与概念的关系 .
同时,我们使用了参考实现 范围-v3 和 cmcstl2型 为了测试支持的概念,他们帮助发现了许多 问题 . S ome公司 是相关的 到 这个 C 概念 s 实施和 一些是其他特性领域中的问题,这些问题是由启用的新编码模式所暴露的 C 概念 s . 我们修复了第一类中的所有问题,并修复了第二类中的大部分问题(其余问题在源代码中解决)。我们现在 编译 运行libr中的所有测试 白羊座 在期间 我们的CI(持续集成) .
测试还帮助暴露了参考实现中的一些源代码问题,我们向库所有者报告了这些问题。
使用C++ 20特性查找库
像我们最近实现的许多其他新特性一样,概念也使用 新的 p arser与语义分析 . 当他们 有很好的报道 我们对质量有信心 经验表明,我们 直到s 有时会看到问题 尤其是当人们开始采用新特性所支持的新编码模式时。
我们一直在寻找大量使用新功能的库。如果你有一些使用 概念或其他C++ 20特性, 请让我们知道,我们 愿意将它们添加到我们的日常RWC(真实世界代码)测试中 . 这将帮助我们改进编译器。
跟我们谈谈!
如果您对VisualStudio中的C++ 20概念支持有反馈,我们将很乐意听取您的意见。我们可以通过下面的评论联系到您。 Y 你可以 也 使用 报告问题 工具或直接访问 Visual Studio开发人员社区 . 你也可以在Twitter上找到我们 @视觉 .