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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:26:09  来源:igfitidea点击:

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

c++cpp-core-guidelinesguideline-support-libraryarray-view

提问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_viewthey 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_viewto span: https://github.com/microsoft/gsl.

我们跟人说话的在库工作组标准委员会。他们希望array_view他们试图进入的标准是只读的。对于核心指南,我们需要一个可读写的抽象。为了避免(潜在)标准和指南支持库(GSL)之间的冲突,我们将我们的(读取和写入)重命名array_viewspanhttps: //github.com/microsoft/gsl

回答by Galik

In the CppCoreGuidlinesThe original array_viewwas 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_viewto span:

来自图书馆进化工作组(LEWG)的文档P0122R(2016-02-12 )正式将类型重命名
array_viewspan

Changelog

Changes from R0

  • Changed the name of the type being proposed from array_viewto spanfollowing feedback from LEWG at the Kona meeting.
  • [...]

变更日志

来自 R0 的变化

  • 在 Kona 会议上将提议的类型名称更改array_viewspan遵循 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 bytetype 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()).

spanhas 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

spanmust 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. spansupports either read-only or mutable access to the sequence it encapsulates. To access read-only data, the user can declare a span<const T>, and access to mutable data would use a span<T>.

[...]

元素类型和转换

span必须通过模板参数配置其元素类型,模板参数ValueType要求是完整的对象类型,而不是抽象类类型。 span支持对其封装的序列进行只读或可变访问。要访问只读数据,用户可以声明一个span<const T>,而访问可变数据将使用一个span<T>

[...]



See also the Guidelines Support Library Review: span<T>from Marius Bancila (march 2016) defining spanas:

另请参阅指南支持图书馆评论: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 as 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::vectoror std::array).

准则支持库是一些在所描述的类型和功能的Microsoft实现C ++核心准则保持在标准C ++基金会。在 GSL 提供的类型中,span<T>以前称为array_view<T>.

span<T>是一个非拥有范围的连续内存,建议用来代替指针(和大小计数器)或标准容器(例如std::vectorstd::array)。