简体   繁体   English

固定大小的std :: span vs std :: array

[英]Fixed-size std::span vs std::array

C++20 includes std::span , which "describes an object that can refer to a contiguous sequence of objects with the first element of the sequence at position zero" . C ++ 20包括std::span ,它“描述了一个对象,该对象可以引用一个连续的对象序列,其中序列的第一个元素位于零位置” Its interface is very close to std::array , though it supports dynamic extent as well as fixed one. 它的接口非常接近std::array ,虽然它支持动态范围和固定范围。

The obvious difference is that std::array owns its elements (and so its destructor destroys them) and std::span doesn't. 明显的区别是std::array拥有它的元素(因此它的析构函数会破坏它们)而std::span则没有。

Is there anything else array can be used for that span can't? 还有什么别的array可以用于那个span不可以吗?

span is to array as pointers are to values. span是指向array指针。

Is there anything an int can be used for than an int* cannot? 有什么的int可以用于比int*不能?

If you swept your code base and replaced every int with an int* you'd have a completely nonsense codebase, even if you added a * at every point-of-use of the int* . 如果你横扫代码库和更换一次intint*你有一个完全胡说八道的代码库,即使你加了*在每个点使用的的int* If you swept your code base and replaced every std::array with a std::span , the same would be true. 如果你扫描代码库并用std::span替换每个std::array ,那么情况也是如此。

Pointers and values are different things. 指针和价值观是不同的东西。 You can jump through hoops and try to deal with pointers as if they are the value of the thing they point to, but trying to do so is often difficult, and the result is often incoherent. 你可以跳过篮球并尝试处理指针,好像它们是他们指向的东西的价值,但试图这样做往往很困难,结果往往是不连贯的。

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

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