专栏首页C++核心准则原文翻译C++核心准则​GSL.view:视图

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

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<T&> does not make sense and is discouraged.

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

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<T>) or marked owner<T*>.

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

  • owner<T*> // a T* that owns the object pointed/referred to; might be nullptr. owner <T *> //一个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<T> differs from a resource handle for a T by still requiring an explicit delete.

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

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

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

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

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

  • not_null<T> // T is usually a pointer type (e.g., not_null<int*> and not_null<owner<Foo*>>) that must not be nullptr. T can be any type for which ==nullptr is meaningful. not_null <T> // T通常是一个指针类型(例如not_null <int *>和not_null <owner <Foo * >>),不能为nullptr。T可以是== nullptr有意义的任何类型。
  • span<T> // [p:p+n), constructor from {p, q} and {p, n}; T is the pointer type span <T> // [p:p + n),{p,q}和{p,n}的构造函数;T是指针类型
  • span_p<T> // {p, predicate} [p:q) where q is the first element for which predicate(*p) is true span_p <T> // {p,谓词} [p:q)其中q是谓词(* p)为true的第一个元素

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

除非T是const类型,否则span <T>表示零个或多个可变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<zstring> for C-style strings that cannot be nullptr. ??? Do we need a name for not_null<zstring>? or is its ugliness a feature?

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

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

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

原始发表时间:2020-11-12

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

我来说两句

0 条评论
登录 后参与评论

相关文章

  • C++核心准则​NL.5:避免在名称中包含类型信息

    If names reflect types rather than functionality, it becomes hard to change the ...

    面向对象思考
  • C++核心准则​讨论:将基类的析构函数设为公共和虚拟的,或受保护的和非虚拟的

    Should destruction behave virtually? That is, should destruction through a point...

    面向对象思考
  • C++核心准则C.183: 不要使用联合体实现双关类型

    It is undefined behavior to read a union member with a different type from the o...

    面向对象思考
  • TTL与LVTTL

    TTL电路是晶体管-晶体管逻辑电路的缩写(Transistor-Transistor Logic),采用双极性工艺(两种载流子)制造,为电流控制元件。

    根究FPGA
  • Codeforces Round #502 (in memory of Leopoldo Taravilse, Div. 1 + Div. 2)C. The Phone Number

    Mrs. Smith is trying to contact her husband, John Smith, but she forgot the secr...

    glm233
  • POJ 3104 Drying

    Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25853 Accepted: 6484

    风骨散人Chiam
  • malloc.c

    glibc-2.14中的malloc.c源代码,供研究malloc和free实现使用:

    一见
  • SQL,PL/SQL 数据类型一览表

    The following is a list of datatypes available in Oracle.

    Leshami
  • SAP CRM扩展字段的元数据仓库

    There are also some other metadata repository tables which would be accessed in ...

    Jerry Wang
  • 【OCP最新题库解析(052)--题29】 For which requirement should you config

    A. accommodating an increasing number of users who run short-duration, infrequen...

    小麦苗DBA宝典

扫码关注云+社区

领取腾讯云代金券