三七草长什么样| 儿童肠胃感冒吃什么药效果好| 苏轼是什么居士| 体内湿气重吃什么食物| 下午5点半是什么时辰| 245是什么意思| 师公是什么意思| onlycook是什么牌子| 好事多磨是什么意思| 星盘是什么| 丑土代表什么| yonex是什么品牌| 属鼠的守护神是什么菩萨| 蛇字五行属什么| 翻盘是什么意思| 小蓝片是什么| braf基因v600e突变是什么意思| 什么是肺大泡| 地球是什么| 拔掉智齿有什么影响| 护理部主任是什么级别| 安之若素是什么意思| 鼻炎吃什么消炎药效果最好| 热伤风吃什么感冒药| 郁郁寡欢什么意思| 减肥喝什么饮料| 秦始皇原名叫什么名字| zw是什么意思| 糖类抗原是检查什么的| 淋巴结肿大是什么引起的| 头发晕是什么病的征兆| 小儿拉肚子吃什么药好得快| 什么鱼有毒| 月经量少吃什么药| 右肺纤维灶是什么意思| 为什么有蟑螂| 6月20号什么星座| 4月10日什么星座| 眼睛长麦粒肿用什么药| 无名指和小指发麻是什么原因| ye是什么颜色| 什么是内卷| 深红色是什么颜色| 出冷汗是什么原因| 你的美丽让你带走是什么歌| 头晕四肢无力是什么原因| 高知是什么意思| 肌酐低是什么意思啊| 手术后为什么要平躺6小时| 疱疹不能吃什么食物| 厅级是什么级别| 12.29是什么星座| 嗓子突然哑了是什么原因引起的| bebe是什么意思| 高铁列车长是什么级别| 胰腺在什么位置| 蛔虫是什么意思| 眼睛痒用什么眼药水好| 泰迪哼哼唧唧表示什么| 什么血型不招蚊子| cs和cf有什么区别| 皮肤挂什么科| c罗全名叫什么| 大黄鸭是什么牌子| 谈婚论嫁什么意思| 为什么会长湿疹| 新鲜橄榄菜长什么样子| 男的叫少爷女的叫什么| 宫腔粘连有什么症状| 工作是为了什么| 手指倒刺是什么原因| 正月十九是什么日子| 青石是什么石头| rh血型阳性是什么血型| 狗感冒了吃什么药| 大明湖畔的夏雨荷是什么意思| 手机充电口叫什么| 复刻版是什么意思| 转移是什么意思| 头眩晕吃什么药| aj和nike什么关系| 左眼皮跳什么意思| 婊子是什么| 肛门疼痛什么原因| 婴儿打嗝是什么原因引起的| 陈皮起什么作用| prog是什么意思| 黄芪可以和什么一起泡水喝| 属猪生什么属相宝宝好| 纸尿裤和拉拉裤有什么区别| 王林为什么叫王麻子| 女人大姨妈来了吃什么最好| 总是打嗝是什么原因引起的| 积聚病什么意思| 药物流产后需要注意什么| ercp是什么意思| 减脂吃什么蔬菜| 肉蔻炖肉起什么作用| 肺部真菌感染用什么药最好| 什么是碳酸饮料| 戒烟后为什么会发胖| 大拇指戴戒指是什么意思| 跳跳糖为什么会跳| 茯苓是什么| 什么叫尿潜血| 晚餐吃什么| 抽烟打嗝是什么情况| 矗读什么| hcg是什么| 大象又什么又什么| 人为什么有五根手指| 肺气肿是什么原因导致的| 女生排卵期是什么时候| 单剂量给药是什么意思| 什么样的花| 肛门指检能查出什么| 鳕鱼不能和什么一起吃| 皮牙子是什么意思| 痛经是什么引起的| 两个人可以玩什么游戏| 上火引起的喉咙痛吃什么药| 刷牙出血是什么原因| 均为是什么意思| 阴道出血是什么样的| 十二指肠溃疡吃什么药| 手为什么会发麻| 尿里有泡沫是什么病| 偏头痛是什么原因引起的| 蛋白过敏是什么症状| 做梦放鞭炮什么意思| 子宫附件彩超检查什么| 耳朵闷闷的堵住的感觉是什么原因| 心跳过快有什么危害| 热毒吃什么药| 兵不血刃的意思是什么| 喝豆浆有什么好处和坏处| 净身出户什么意思| 吃茴香有什么好处和坏处| 无痛人流后吃什么对身体恢复比较好| 戒指带中指什么意思| 眼白发红是什么原因| 经常咬手指甲是什么原因| 为什么有的人招蚊子| 为什么会便血| 全身疼是什么病| 拔苗助长告诉我们什么道理| 夏天喝什么解暑| 白带是什么意思| 大头鱼吃什么食物| au750是什么意思| 89岁属什么生肖| 下肢动脉硬化吃什么药| 鼻窦粘膜增厚什么意思| 频繁流鼻血是什么原因| 什么地爬| 晚上睡觉盗汗是什么原因| 痔疮什么感觉| 硅橡胶是什么材料| 五月十一是什么星座| 情人节送妈妈什么花| 4月16日是什么星座| 梦见自己怀孕大肚子是什么预兆| 电脑长期不关机有什么影响| 走路气喘是什么原因| 今天属什么生肖老黄历| 出汗臭是什么原因| 青椒炒什么好吃又简单| 食管反流吃什么药最好| 平起平坐是什么动物| 藏红花泡水是什么颜色| 梦见买豆腐是什么意思| 吊什么意思| 千什么百什么| 白细胞低说明什么| 运钞车押运员是什么人| 狗取什么名字好| 喝酒前喝什么不容易醉又不伤胃| 海带炖什么好吃| 口腔挂什么科| 明天是什么日子| 疱疹不能吃什么食物| 事业是什么意思| 溜达鸡是什么意思| 护士要什么学历| 可可粉是什么东西| 南极为什么比北极冷| 梦见别人受伤流血是什么预兆| 视功能是什么| 慢性胃炎吃什么药好| 宗气是什么意思| 脚麻什么原因| 吃什么东西可以养胃| 指甲盖发紫是什么原因| 女孩喜欢什么礼物| gs是什么| 脚环肿是什么原因引起的| 皮肤一块白一块白的是什么原因| 内膜薄吃什么补得最快| 出栏是什么意思| 五分类血常规检查什么| 干咳喝什么止咳糖浆好| 回迁是什么意思| 奶咖色是什么颜色| 64年出生属什么| 血糖高初期有什么症状| 吃什么油最好| 脊柱侧弯挂什么科| 阳历一月份是什么星座| 微信什么时候推出的| 东北小咬是什么虫子| 天天都需要你爱是什么歌| 504是什么错误| 血酮体高代表什么| 半月板退变是什么意思| 脾胃虚弱吃什么药最好| hpv59高危阳性是什么意思| 吃什么生发效果最好| 南北杏和什么煲汤止咳化痰| 桑拓木命是什么意思| 关羽姓什么| 造化什么意思| 广州有什么特产必带| 打呼噜吃什么| 中药吃多了对人体有什么伤害| 心服口服是什么意思| 维生素d低是什么原因| 什么是房补| ;是什么号| 葡萄像什么比喻句| 洋葱生吃有什么好处| 隐忍是什么意思| 枸杞和什么搭配壮阳| 蝗虫吃什么| 陈凯歌为什么不娶倪萍| 分泌是什么意思| 头麻是什么病的前兆| 喝什么饮料解酒最快最有效| 锋字五行属什么| 头出虚汗是什么原因引起的| 护理和护士有什么区别| 苏打水什么牌子的好| 公约是什么意思| 发烧有什么症状| 山楂泡水喝有什么好处| 为什么眼睛会肿而且痛| 口干口臭是什么原因引起的| 什么样的阳光填形容词| 贝壳吃什么| 补铁的药什么时候吃最好| 南极和北极有什么区别| 福利院是干什么的| 哺乳期胃疼可以吃什么药| 睡觉做梦多是什么原因| redline是什么牌子| 总想喝水是什么原因| 打破伤风不能吃什么| 什么是飞蚊症| 呼吸衰竭是什么意思| 蟠桃为什么是扁的| 液化气是什么| 干咳喝什么药| 儿童坐飞机需要什么证件| 医院介入科是干什么的| 苹果a1661是什么型号| 百度

解读丨4399《疯狂的子弹》生化危机番外篇独家曝光

百度 ”△八里庄支撑着张豪依然在关注这个区域的唯一原因是——7号线地铁开通了。

Concepts are an extension to the templates feature provided by the C++ programming language. Concepts are named Boolean predicates on template parameters, evaluated at compile time. A concept may be associated with a template (class template, function template, member function of a class template, variable template, or alias template), in which case it serves as a constraint: it limits the set of arguments that are accepted as template parameters.

Originally dating back to suggestions for C++11, the original concepts specification has been revised multiple times before formally being a required part of C++20.

Main uses

edit

The main uses of concepts are:

  • introducing type-checking to template programming
  • simplified compiler diagnostics for failed template instantiations
  • selecting function template overloads and class template specializations based on type properties
  • constraining automatic type deduction

Constraint types and usage

edit

There are five different places in a function template signature where a constraint can be used (labeled below as C1 to C5):[1]

template<C1 T>
requires C2<T>
C3 auto Fun(C4 auto param) requires C5<T>;
  • C1: A type-constraint. This kind replaces class or typename for declaring a type template parameter. When using a concept instead of the former two the type is constraint.
  • C2: A requires-clause. Whenever a type-constraint does not work, for example, because the concept takes multiple parameters, a requires-clause can be used to apply more elaborated constraints.
  • C3 / C4: A constrained placeholder type. The same syntax is available for placeholder variable aka. auto variable. C++20 added abbreviated function templates which use auto as a placeholder type in the parameter declaration.[2] A constrained placeholder type allows to put constraints on the automatically deduced return type of a function or a variable.
  • C5: A trailing requires-clause. This form is similar to C2 with one notable exception. A trailing requires-clause can be applied to a function in a class template. This allows the function to remain a regular, template-free function, which can be enabled or disabled depending on the functions trailing requires-clause.

The constraint forms C1 and C2 can be used in all kinds of templates.

Example: equality_comparable

edit

The following is a declaration of the concept "equality_comparable" from the <concepts> header of a C++20 standard library. This concept is satisfied by any type T such that for lvalues a and b of type T, the expressions a==b and a!=b as well as the reverse b==a and b!=a compile, and their results are convertible to a type that satisfies the concept "boolean-testable":

// The following concept is an implementation detail used to build equality_comparable
template<typename T, typename U>
concept weakly_equality_comparable_with = requires(const remove_reference<T>& a, const remove_reference<U>& b) {
    { a == b } -> std::same_as<bool>;
    { a != b } -> std::same_as<bool>;
    { b == a } -> std::same_as<bool>;
    { b != a } -> std::same_as<bool>;
};

template<class T>
concept equality_comparable = weakly_equality_comparable_with<T, T>;

A function template constrained on this concept may be declared as follows:

void f(const equality_comparable auto&); // constrained abbreviated function template declaration using a constrained placeholder type (C4 from above)

or

template <equality_comparable T>
void f(const T&); // constrained function template declaration using a type constraint (C1 from above)

And may be called as usual:

f(42); // OK, int satisfies equality_comparable

Compiler diagnostics

edit

If a programmer attempts to use a template argument that does not satisfy the requirements of the template, the compiler will generate an error. When concepts are not used, such errors are often difficult to understand because the error is not reported in the context of the call, but rather in an internal, often deeply nested, implementation context where the type was used.

For example, std::sort requires that its first two arguments be random-access iterators. If an argument is not an iterator, or is an iterator of a different category, an error will occur when std::sort attempts to use its parameters as bidirectional iterators:

// std::list is typically a doubly-linked list, whose iterators are not random-access
std::list<int> l = {2, 1, 3};
std::sort(l.begin(), l.end());

Typical compiler diagnostic without concepts is over 50 lines of output, beginning with a failure to compile an expression that attempts to subtract two iterators:

In instantiation of 'void std::__sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = std::_List_iterator<int>; _Compare = __gnu_cxx::__ops::_Iter_less_iter]':
 error: no match for 'operator-' (operand types are 'std::_List_iterator<int>' and 'std::_List_iterator<int>')
 std::__lg(__last - __first) * 2,

[..]

If concepts are used, the error can be detected and reported in the context of the call:

error: cannot call function 'void std::sort(_RAIter, _RAIter) [with _RAIter = std::_List_iterator<int>]'
note:   concept 'RandomAccessIterator()' was not satisfied

Overload resolution

edit

Concepts can be used to choose function template overloads and class template specializations based on properties of their template arguments, as an alternative to SFINAE and tag dispatching. If an argument satisfies more than one concept, the overload associated with the more constrained concept is chosen.

Type deduction

edit

Concepts may be used instead of the unconstrained type deduction placeholder auto in variable declarations and function return types:

auto     x1 = f(y); // the type of x1 is deduced to whatever f returns
Sortable auto x2 = f(y); // the type of x2 is deduced, but only compiles if it satisfies Sortable

Implementation status

edit

Concepts TS, as specified in ISO/IEC TS 19217:2015, are implemented as an experimental feature in GCC 6.[3] C++20 concepts are fully implemented in GCC 10,[4] MSVC 19.30,[5] and Clang 10.[6]

History

edit

A different form of Concepts, popularly known as "C++0x Concepts", was temporarily accepted into the working paper for C++11 but was removed in 2009.[7] In addition to concepts themselves, "C++0x Concepts" included concept maps (a feature that could make it possible, for example, for the concept "Stack" to accept std::vector, automatically mapping "Stack" operations such as push() to differently named operations on std::vector, such as push_back()) and axioms (a facility to specify semantic properties such as associativity or commutativity, allowing the compiler to take advantage of these properties without proof).

In contrast to this abandoned proposal, the C++20 version of Concepts is sometimes referred to as "Concepts Lite".[8]

During the C++ standards committee meeting in March 2016, the evolution working group moved to merge Concepts into the mainline C++17 standard, but the motion was defeated in full committee.[9]

Concepts v1 was merged into the C++20 draft.[10]

"The One Range" version of Range feature that depend on concepts was also merged into C++20.

See also

edit

Notes

edit
  1. ^ Fertig, Andreas (2021). Programming with C++20. Fertig Publications. p. 23. ISBN 978-3-949323-01-0.
  2. ^ "ISO/IEC 14882:2020". ISO. December 2020. Retrieved 14 July 2022.
  3. ^ "GCC 6 Release Series - Changes, New Features, and Fixes".
  4. ^ "C++ compiler support (gcc)".
  5. ^ "C++ compiler support".
  6. ^ "C++ Support in Clang".
  7. ^ Bjarne Stroustrup (22 July 2009). "The C++0x "Remove Concepts" Decision". Dr. Dobbs.
  8. ^ Andrew Sutton (24 February 2013). "Concepts Lite: Constraining Templates with Predicates". isocpp.org.
  9. ^ Honermann, Tom (6 March 2016). "Why Concepts didn't make C++17". honermann.net. Archived from the original on 2 October 2018. Retrieved 19 April 2016.
  10. ^ "2017 Toronto ISO C++ Committee Discussion Thread (Concepts in C++20; Coroutines, Ranges and Networking TSes published) : cpp". 15 July 2017.

References

edit
edit
痔疮嵌顿是什么意思 撕脱性骨折什么意思 吃番茄有什么好处 榨菜是什么菜 高血压不能吃什么
黑鱼吃什么食物 毓婷和金毓婷有什么区别 血容量不足是什么意思 日月星辰是什么意思 十月一日是什么日子
端的是什么意思 ons是什么意思 最熟悉的陌生人是什么意思 胸口长痘痘是什么原因 工资5k是什么意思
梦见被狗咬是什么意思 在此是什么意思 情人眼里出西施是什么心理效应 1992年属猴的是什么命 鸢是什么意思
近视是什么sanhestory.com 刀个刀个刀刀那是什么刀hcv8jop6ns9r.cn 教学相长是什么意思hcv9jop2ns4r.cn 库欣综合征是什么病hcv8jop7ns4r.cn 膝关节痛什么原因hcv9jop4ns9r.cn
空腹血糖17已经严重到什么时候hcv8jop1ns4r.cn 什么中生什么liaochangning.com 荔枝是什么季节的水果hcv8jop5ns3r.cn 觉的部首是什么偏旁hcv7jop7ns4r.cn 讨扰是什么意思hcv8jop7ns7r.cn
白月光什么意思hanqikai.com 膈肌痉挛吃什么药hcv9jop5ns7r.cn 新生儿头发稀少是什么原因hcv9jop5ns6r.cn 6.25是什么星座hcv8jop0ns8r.cn 小螳螂吃什么hcv8jop4ns7r.cn
杭州五行属什么hcv9jop8ns1r.cn nda是什么意思dajiketang.com 海澜之家属于什么档次hcv9jop5ns7r.cn au585是什么金naasee.com 口咸是什么原因引起的hcv7jop5ns0r.cn
百度