gsl 库中的 span 和 array_view 有什么区别?
What's the difference between span and array_view in the gsl library?
在最近的几次会议演讲中,我听到 Bjarne Stroustrup 和其他人提到了 C++ 的新编码指南和一些支持它们的类型。
具体我记得the example of span<T>
instead of (T* p, int n)
as a parameter to a function(当时约32:00进入讲);但我还记得使用 array_view<T>
的建议。它们是两种选择但相同的概念吗?还是我把事情弄糊涂了,而它们实际上并没有那么相关?
我似乎找不到关于它们应该是什么的任何权威定义。
在CppCoreGuidlines原来的array_view
改名为span
。
参见:https://github.com/isocpp/CppCoreGuidelines/pull/377
是这样描述的:
span is a bounds-checked, safe alternative to using pointers to access arrays
我们与 library working group in 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 中的人进行了交谈。
P0122R (2016-02-12) 来自 Library Evolution Working Group (LEWG)
正式将类型 array_view
重命名为 span
:
Changelog
Changes from R0
- Changed the name of the type being proposed from
array_view
to span
following feedback from LEWG at the Kona meeting.
- [...]
我们还可以阅读:
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.
在下一章中,本文档介绍了只读和读写(可变) 访问:
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
supports 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>
.
[...]
另见 Marius Bancila 的 Guidelines Support Library Review: span<T>
(2016 年 3 月)将 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.
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::vector
or std::array
).
在最近的几次会议演讲中,我听到 Bjarne Stroustrup 和其他人提到了 C++ 的新编码指南和一些支持它们的类型。
具体我记得the example of span<T>
instead of (T* p, int n)
as a parameter to a function(当时约32:00进入讲);但我还记得使用 array_view<T>
的建议。它们是两种选择但相同的概念吗?还是我把事情弄糊涂了,而它们实际上并没有那么相关?
我似乎找不到关于它们应该是什么的任何权威定义。
在CppCoreGuidlines原来的array_view
改名为span
。
参见:https://github.com/isocpp/CppCoreGuidelines/pull/377
是这样描述的:
span is a bounds-checked, safe alternative to using pointers to access arrays
我们与 library working group in 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 中的人进行了交谈。
P0122R (2016-02-12) 来自 Library Evolution Working Group (LEWG)
正式将类型 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.- [...]
我们还可以阅读:
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.
在下一章中,本文档介绍了只读和读写(可变) 访问:
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>
.[...]
另见 Marius Bancila 的 Guidelines Support Library Review: span<T>
(2016 年 3 月)将 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. 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
).