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
引用永远都不是所有者(请参阅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
假定“原始指针”表示法(例如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
owner
An owner
假定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
除非T是const类型,否则span
"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
对于不能为nullptr的C样式字符串,请使用not_null
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#gslview-views
新书介绍
《实战Python设计模式》是作者最近出版的新书,拜托多多关注!
本书利用Python 的标准GUI 工具包tkinter,通过可执行的示例对23 个设计模式逐个进行说明。这样一方面可以使读者了解真实的软件开发工作中每个设计模式的运用场景和想要解决的问题;另一方面通过对这些问题的解决过程进行说明,让读者明白在编写代码时如何判断使用设计模式的利弊,并合理运用设计模式。
对设计模式感兴趣而且希望随学随用的读者通过本书可以快速跨越从理解到运用的门槛;希望学习Python GUI 编程的读者可以将本书中的示例作为设计和开发的参考;使用Python 语言进行图像分析、数据处理工作的读者可以直接以本书中的示例为基础,迅速构建自己的系统架构。
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!