简体   繁体   English

gsl库中span和array_view的区别是什么?

[英]What's the difference between span and array_view in the gsl library?

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); 具体来说,我记得span<T>的例子,而不是(T* p, int n)作为函数的参数 (在谈话时间大约32:00); but I also remember the suggestion to use array_view<T> . 但我也记得使用array_view<T>的建议。 Are they two alternatives but the same concept? 它们是两个替代方案但是相同的概念吗? Or am I confusing things and they're actually not so related? 或者我是否混淆了事情,他们实际上并没有那么相关?

I can't seem to find any authoritative definition of what they're both supposed to be about. 我似乎无法找到任何关于它们应该是什么的权威定义。

We talked with people in the library working group in the standards committee . 我们与标准委员会 图书馆工作组的人员进行了交谈。 They wanted the array_view they are trying to get into the standard to be read only. 他们想要array_view他们试图进入标准只读。 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 . 为了避免(潜在)标准与指南支持库(GSL)之间的冲突,我们将我们的(读取和写入) array_view重命名为spanhttps//github.com/microsoft/gsl

In the CppCoreGuidlines The 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是使用指针访问数组的边界检查,安全的替代方法

The document P0122R (2016-02-12) from the Library Evolution Working Group (LEWG) 来自图书馆演变工作组 (LEWG)的文件P0122R (2016-02-12)
officially renames the type array_view to span : 正式将array_view类型重命名为span

Changelog 更新日志

Changes from R0 从R0的变化

  • Changed the name of the type being proposed from array_view to span following feedback from LEWG at the Kona meeting. 根据LEWG在Kona会议上的反馈,将array_view中提议类型的名称更改为span
  • [...] [...]

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 . 如果可能取决于byte类型并改变P0257中提出的类型别名行为,它将得到增强。

However – if adopted – it may be useful to overload some standard library functions for this new type (an example would be copy() ). 但是 - 如果采用 - 为这种新类型重载某些标准库函数可能很有用(例如, 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. span已在标准C ++(C ++ 11)中实现,并且已成功用于C ++代码的商业静态分析工具以及商业办公生产力软件。 An open source, reference implementation is available at https://github.com/Microsoft/GSL . https://github.com/Microsoft/GSL上提供了一个开源的参考实现。

In a next chapter, this documents presents the read-only and read-write ( mutable ) accesses: 在下一章中,本文档介绍了只读读写可变 )访问:

Element types and conversions 元素类型和转换

span must be configured with its element type via the template parameter ValueType , which is required to be a complete object type that is not an abstract class type. span必须通过模板参数ValueType配置其元素类型,该参数必须是不是抽象类类型的完整对象类型。 span supports either read-only or mutable access to the sequence it encapsulates. span支持对其封装的序列进行只读或可变访问。 To access read-only data, the user can declare a span<const T> , and access to mutable data would use a span<T> . 要访问只读数据,用户可以声明span<const T> ,并且对可变数据的访问将使用span<T>

[...] [...]


See also the Guidelines Support Library Review: span<T> from Marius Bancila (march 2016) defining span as: 另见指南支持库评论:来自Marius Bancila(2016年3月)的span<T>定义span为:

The Guidelines Support Library is a Microsoft implementation of some of the types and functions described in the C++ Core Guidelines maintained by the Standard C++ Foundation . 指南支持库是Microsoft 标准C ++基础维护的C ++核心指南中描述的某些类型和功能的Microsoft实现。 Among the types provided by the GSL is span<T> formerly known as array_view<T> . GSL提供的类型之一是span<T>以前称为array_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 as std::vector or std::array ). span<T>是一个非拥有的连续内存范围,建议用于代替指针(和大小计数器)或标准容器(如std::vectorstd::array )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM