C++核心准则​GSL.view:视图

面向对象思考

共 4568字,需浏览 10分钟

 ·

2020-11-13 10:39

GSL.view: Views

GSL.view:视图

These types allow the user to distinguish between owning and non-owning pointers and between pointers to a single object and pointers to the first element of a sequence.

这些类型使用户可以区分拥有和不拥有的指针,以及指向单个对象的指针和指向序列的第一个元素的指针。

These "views" are never owners.

这里的各种“view”绝不是所有者。

References are never owners (see R.4. Note: References have many opportunities to outlive the objects they refer to (returning a local variable by reference, holding a reference to an element of a vector and doing push_back, binding to std::max(x, y + 1), etc. The Lifetime safety profile aims to address those things, but even so owner does not make sense and is discouraged.

引用永远都不是所有者(请参阅R.4.注意:引用有很多机会使它们引用的对象寿命更长(通过引用返回局部变量,持有对vector元素的引用并进行push_back,绑定到std :: max(x,y + 1)等)。生命周期安全规则群组旨在解决这些问题,但是即使如此,owner 也没有意义,因此不建议使用。

The names are mostly ISO standard-library style (lower case and underscore):

名称主要是ISO标准库样式(小写和下划线):

  • T* // The T* is not an owner, might be null; assumed to be pointing to a single element.

    T * // T *不是所有者,可以为null;假定指向单个元素。

  • T& // The T& is not an owner and can never be a "null reference"; references are always bound to objects.

    T&// T&不是所有者,永远不能是“空引用”;引用始终绑定到对象。

The "raw-pointer" notation (e.g. int*) is assumed to have its most common meaning; that is, a pointer points to an object, but does not own it. Owners should be converted to resource handles (e.g., unique_ptr or vector) or marked owner.

假定“原始指针”表示法(例如int *)具有最常见的含义;也就是说,指针指向一个对象,但不拥有它。所有者应转换为资源句柄(例如,unique_ptr或vector )或标记为所有者

  • owner // a T* that owns the object pointed/referred to; might be nullptr.

    owner //一个T *,它拥有指向/引用的对象;可能为nullptr。

owner is used to mark owning pointers in code that cannot be upgraded to use proper resource handles. Reasons for that include:

owner用于在无法升级为使用适当资源句柄的代码中标记所有者指针。原因包括:

  • Cost of conversion.

    转换成本。

  • The pointer is used with an ABI.

    该指针与ABI一起使用。

  • The pointer is part of the implementation of a resource handle.

    指针是资源句柄实现的一部分。

An owner differs from a resource handle for a T by still requiring an explicit delete.

owner 与T的资源句柄不同,它仍然需要显式删除。

An owner is assumed to refer to an object on the free store (heap).

假定owner 引用自由存储(堆)上的对象。

If something is not supposed to be nullptr, say so:

如果不应该使用nullptr,请这样说:

  • not_null // T is usually a pointer type (e.g., not_null and not_null>) that must not be nullptr. T can be any type for which ==nullptr is meaningful.

    not_null // T通常是一个指针类型(例如not_null 和not_null >),不能为nullptr。T可以是== nullptr有意义的任何类型。

  • span // [p:p+n), constructor from {p, q} and {p, n}; T is the pointer type

    span // [p:p + n),{p,q}和{p,n}的构造函数;T是指针类型

  • span_p // {p, predicate} [p:q) where q is the first element for which predicate(*p) is true

    span_p // {p,谓词} [p:q)其中q是谓词(* p)为true的第一个元素

A span refers to zero or more mutable Ts unless T is a const type.

除非T是const类型,否则span 表示零个或多个可变Ts。

"Pointer arithmetic" is best done within spans. A char* that points to more than one char but is not a C-style string (e.g., a pointer into an input buffer) should be represented by a span.

最好在范围完成“指针算术”。指向多个char但不是C样式字符串的char *(例如,指向输入缓冲区的指针)应以span表示。

  • zstring // a char* supposed to be a C-style string; that is, a zero-terminated sequence of char or nullptr

    zstring //一个char *,应该是C样式的字符串;即char或nullptr的零终止序列

  • czstring // a const char* supposed to be a C-style string; that is, a zero-terminated sequence of const char or nullptr

    czstring //一个const char *,应该是C样式的字符串;也就是说,一个以零结尾的const char或nullptr序列

Logically, those last two aliases are not needed, but we are not always logical, and they make the distinction between a pointer to one char and a pointer to a C-style string explicit. A sequence of characters that is not assumed to be zero-terminated should be a char*, rather than a zstring. French accent optional.

从逻辑上讲,不需要最后两个别名,但是我们并不总是合乎逻辑的,它们使指向一个char的指针和指向C样式字符串的指针之间的区别变得明确。不假定以零结尾的字符序列应该是char *,而不是zstring。法国口音可选。

Use not_null for C-style strings that cannot be nullptr. ??? Do we need a name for not_null? or is its ugliness a feature?

对于不能为nullptr的C样式字符串,请使用not_null 。???我们需要一个not_null 的名称吗?还是它的丑陋功能?


原文链接

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslview-views


新书介绍

《实战Python设计模式》是作者最近出版的新书,拜托多多关注!

本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。

对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。




觉得本文有帮助?请分享给更多人。

关注微信公众号【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!



浏览 40
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报