C++ gsl 库中的span 和array_view 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34832090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the difference between span and array_view in the gsl library?
提问by einpoklum
In several recent conference presentation I've heard Bjarne Stroustrup and others mention new coding guidelines for C++ and some types supporting them.
在最近的几次会议演讲中,我听到 Bjarne Stroustrup 和其他人提到了 C++ 的新编码指南和支持它们的一些类型。
Specifically, I remember the example of span<T>
instead of (T* p, int n)
as a parameter to a function(at time about 32:00 into the talk); but I also remember the suggestion to use array_view<T>
. Are they two alternatives but the same concept? Or am I confusing things and they're actually not so related?
具体而言,记得的例子span<T>
,而不是(T* p, int n)
作为一个参数的函数(在时间大约32:00到谈话); 但我也记得使用array_view<T>
. 它们是两种选择但相同的概念吗?或者我是否混淆了事情而它们实际上并没有那么相关?
I can't seem to find any authoritative definition of what they're both supposed to be about.
我似乎找不到任何关于它们应该是什么的权威定义。
回答by Bjarne Stroustrup
We talked with people in the library working groupin the standards committee. They wanted the array_view
they are trying to get into the standard to be read only. For the core guidelines, we needed an abstraction that was read and write. To avoid a clash between the (potential) standards and the guidelines support library (GSL), we renamed our (read and write) array_view
to span
: https://github.com/microsoft/gsl.
我们跟人说话的在库工作组在标准委员会。他们希望array_view
他们试图进入的标准是只读的。对于核心指南,我们需要一个可读写的抽象。为了避免(潜在)标准和指南支持库(GSL)之间的冲突,我们将我们的(读取和写入)重命名array_view
为span
:https: //github.com/microsoft/gsl。
回答by Galik
In the CppCoreGuidlinesThe original array_view
was renamed to span
.
在CppCoreGuidlines 中,原来的array_view
被重命名为span
.
See: https://github.com/isocpp/CppCoreGuidelines/pull/377
请参阅:https: //github.com/isocpp/CppCoreGuidelines/pull/377
It is described thus:
是这样描述的:
span is a bounds-checked, safe alternative to using pointers to access arrays
span 是使用指针访问数组的边界检查的安全替代方法
回答by olibre
The document P0122R(2016-02-12) from the Library Evolution Working Group(LEWG)
officially renames the type array_view
to span
:
来自图书馆进化工作组(LEWG)的文档P0122R(2016-02-12 )正式将类型重命名为:array_view
span
Changelog
Changes from R0
- Changed the name of the type being proposed from
array_view
tospan
following feedback from LEWG at the Kona meeting.- [...]
变更日志
来自 R0 的变化
- 在 Kona 会议上将提议的类型名称更改
array_view
为span
遵循 LEWG 的反馈。- [...]
We can also read:
我们还可以阅读:
Impact on the Standard
This proposal is a pure library extension. It does not require any changes to standard classes, functions, or headers. It would be enhanced if could depends on the
byte
type and changes to type aliasing behavior proposed in P0257.However – if adopted – it may be useful to overload some standard library functions for this new type (an example would be
copy()
).
span
has been implemented in standard C++ (C++11) and is being successfully used within a commercial static analysis tool for C++ code as well as commercial office productivity software. An open source, reference implementation is available at https://github.com/Microsoft/GSL.
对标准的影响
这个提议是一个纯粹的库扩展。它不需要对标准类、函数或标头进行任何更改。如果可以取决于P0257 中
byte
提议的类型和对类型别名行为的更改,它将得到增强。但是——如果采用——为这种新类型重载一些标准库函数可能会很有用(一个例子是
copy()
)。
span
已在标准 C++ (C++11) 中实现,并成功用于 C++ 代码的商业静态分析工具以及商业办公生产力软件。开源参考实现可从https://github.com/Microsoft/GSL 获得。
In a next chapter, this documents presents the read-onlyand read-write(mutable) accesses:
在下一章中,本文档介绍了只读和读写(可变)访问:
Element types and conversions
span
must be configured with its element type via the template parameterValueType
, which is required to be a complete object type that is not an abstract class type.span
supports either read-only or mutable access to the sequence it encapsulates. To access read-only data, the user can declare aspan<const T>
, and access to mutable data would use aspan<T>
.[...]
元素类型和转换
span
必须通过模板参数配置其元素类型,模板参数ValueType
要求是完整的对象类型,而不是抽象类类型。span
支持对其封装的序列进行只读或可变访问。要访问只读数据,用户可以声明一个span<const T>
,而访问可变数据将使用一个span<T>
。[...]
See also the Guidelines Support Library Review: span<T>
from Marius Bancila (march 2016) defining span
as:
另请参阅指南支持图书馆评论:span<T>
来自 Marius Bancila(2016 年 3 月),定义span
为:
The Guidelines Support Libraryis a Microsoft implementation of some of the types and functions described in the C++ Core Guidelinesmaintained by the Standard C++ Foundation. Among the types provided by the GSL is
span<T>
formerly known asarray_view<T>
.
span<T>
is a non-owning range of contiguous memory recommended to be used instead of pointers (and size counter) or standard containers (such asstd::vector
orstd::array
).
该准则支持库是一些在所描述的类型和功能的Microsoft实现C ++核心准则保持在标准C ++基金会。在 GSL 提供的类型中,
span<T>
以前称为array_view<T>
.
span<T>
是一个非拥有范围的连续内存,建议用来代替指针(和大小计数器)或标准容器(例如std::vector
或std::array
)。