专栏首页C++核心准则原文翻译C++核心准则T.13:对于简单的,单类型参数概念,使用缩略记法更好

C++核心准则T.13:对于简单的,单类型参数概念,使用缩略记法更好

月季

T.13: Prefer the shorthand notation for simple, single-type argument concepts

T.13:对于简单的,单类型参数概念,使用缩略记法更好

Reason(原因)

Readability. Direct expression of an idea.

可读性。直接表现想法。

Example (using TS concepts)示例(使用TS概念)

To say "T is Sortable":

为了表达“T是可排序类型”:

template<typename T>       // Correct but verbose: "The parameter is
//    requires Sortable<T>   // of type T which is the name of a type
void sort(T&);             // that is Sortable"

template<Sortable T>       // Better (assuming support for concepts): "The parameter is of type T
void sort(T&);             // which is Sortable"

void sort(Sortable&);      // Best (assuming support for concepts): "The parameter is Sortable"

The shorter versions better match the way we speak. Note that many templates don't need to use the template keyword.

较短的版本更符合我们想要表达的。注意很多模板不需要使用模板关键字。

Note(注意)

"Concepts" are defined in an ISO Technical Specification: concepts. A draft of a set of standard-library concepts can be found in another ISO TS: ranges Concepts are supported in GCC 6.1 and later. Consequently, we comment out uses of concepts in examples; that is, we use them as formalized comments only. If you use a compiler that supports concepts (e.g., GCC 6.1 or later), you can remove the //

“概念”在ISO技术规格concepts中被定义。一套标准库concepts的初步版本可以在另一个ISO技术规格:ranges中找到。GCC6.1以后都支持concepts。因此我们在实例代码中注释掉使用concepts的部分;也就是说我们只是将它们用作标准的注释。如果你使用GCC6.1之后的版本,可以打开注释。

Enforcement(实施建议)

  • Not feasible in the short term when people convert from the <typename T> and <class T> notation.
  • 如果人们从<typename T> 和<class T>记法转过来,使用缩略记法是不合适的。
  • Later, flag declarations that first introduce a typename and then constrain it with a simple, single-type-argument concept.
  • 随后,标记第一次引入类型名并马上使用简单的,单类型概念对其进行约束的情况。

原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t13-prefer-the-shorthand-notation-for-simple-single-type-argument-concepts

本文分享自微信公众号 - 面向对象思考(OOThinkingDalian),作者:面向对象思考

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2020-08-25

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C++核心准则Per.7:设计要为优化做准备

    Because we often need to optimize the initial design. Because a design that igno...

    面向对象思考
  • C++核心准则CP.4:按照任务思考问题,而不是线程

    CP.4: Think in terms of tasks, rather than threads

    面向对象思考
  • C++核心准则E.25:如果不能抛出异常,模仿RAII方式进行资源管理

    Even without exceptions, RAII is usually the best and most systematic way of dea...

    面向对象思考
  • 【CodeForces 621C】Wet Shark and Flowers

    There are n sharks who grow flowers for Wet Shark. They are all sitting around t...

    饶文津
  • 军事仿真评估:评估标准的分类(CS CY)

    在军事模拟领域,有很多不同的方法。 近战战术教练机、联合战术战术训练系统、战斗力战术训练或作战人员模拟2000只是美国国防部大型建模与仿真开发项目历史上的一些例...

    用户7095611
  • 基于tensorflow 1.0的图像叙事功能测试(model/im2txt)

    作为多模态数据处理的经典,图像自动打标签(图像叙事功能)一直是一项非常前沿的技术,涉及到机器视觉,自然语言处理等模块。 幸运的是,谷歌基于tensorflow将...

    sparkexpert
  • 关于PCR引物设计原则

    1. Primer Length: It is generally accepted that the optimal length of PCR primer...

    用户7625144
  • Q122 Best Time to Buy and Sell Stock II

    Say you have an array for which the ith element is the price of a given stock on...

    echobingo
  • FAST Algorithm for Corner Detection

    We saw several feature detectors and many of them are really good. But when look...

    bear_fish
  • 3道线段树题

    A city's skyline is the outer contour of the silhouette formed by all the buildi...

    王脸小

扫码关注云+社区

领取腾讯云代金券