简体   繁体   中英

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" . Its interface is very close to std::array , though it supports dynamic extent as well as fixed one.

The obvious difference is that std::array owns its elements (and so its destructor destroys them) and std::span doesn't.

Is there anything else array can be used for that span can't?

span is to array as pointers are to values.

Is there anything an int can be used for than an int* cannot?

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* . If you swept your code base and replaced every std::array with a std::span , the same would be true.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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