Questions tagged [std-span]

A non-owning lightweight wrapper object referring to a contiguous sequence of elements in memory

60 questions
411
votes
4 answers

What is a "span" and when should I use one?

Recently I've gotten suggestions to use span's in my code, or have seen some answers here on the site which use span's - supposedly some kind of container. But - I can't find anything like that in the C++17 standard library. So what is this…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
21
votes
1 answer

Why does std::span lack the comparison operators?

Wasn't the std::span designed as a lightweight reference to sub-regions of std::vector/std::array/plain array and alike? Shouldn't it also contain comparison operators in its API, to be consistent with them? What was the reasoning behind the…
GreenScape
  • 7,191
  • 2
  • 34
  • 64
19
votes
2 answers

Does span propagate const?

The standard containers propagate const. That is, their elements are automatically const if the containers themselves are const. For example: const std::vector vec{3, 1, 4, 1, 5, 9, 2, 6}; ranges::fill(vec, 314); // impossible const std::list…
L. F.
  • 19,445
  • 8
  • 48
  • 82
16
votes
1 answer

Does C++ allow comparison between std::span::iterators when one span is a subspan of the other?

Generally speaking C++ doesn't allow comparing iterators between different containers. For example: int main() { std::vector v = {1, 2, 3}; std::vector w = {4, 5, 6}; std::cout << v.end() == w.end() << std::endl; //…
Maks Verver
  • 661
  • 4
  • 14
16
votes
1 answer

Why are span's array and std::array constructors different from its container constructors

I have been playing around with the latest specification for std::span using the clang trunk and libc++ on Godbolt and find some of the constructors confusing. In particular I find the constructors from a plain old array and and a std::array to be…
John M
  • 163
  • 5
14
votes
1 answer

Why does std::span lack cbegin and cend methods?

Why does std::span only have begin and end methods and not their constant iterator counterparts cbegin and cend? (standard) What I noticed too is that the proposals for span that I could find do have a definition for cbegin and cend: P0122R7. Why…
SWdV
  • 1,715
  • 1
  • 15
  • 36
12
votes
3 answers

Why in particular should I rather pass a std::span than a std::vector& to a function?

I know this might overlap with the question What is a “span” and when should I use one?, but I think the answer to this specific part of the question is pretty confusing. On one hand, there are quotes like this: Don't use it if you have a standard…
Tom Gebel
  • 744
  • 1
  • 4
  • 13
11
votes
1 answer

Return conditional `range_expression`

What's the most efficient way of iterating over one of several known ranges based on some condition? pseudo-code for a binary condition: for element in (condition ? range_a : range_b) // do work This 'example' shows my intention using a…
Tom
  • 3,281
  • 26
  • 33
10
votes
2 answers

Simplest way to assign std::span to std::vector

I wanted to do this #include #include struct S { std::vector v; void set(std::span _v) { v = _v; } }; But it does not compile. What are the alternatives?
tuket
  • 3,232
  • 1
  • 26
  • 41
10
votes
1 answer

What happened to std::cspan?

std::span has been voted into C++20. I assumed that along with std::span, there would be a convenience alias defined like this: template using cspan = span; To me, this seems like a really…
Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
10
votes
1 answer

Why is std::span a pointer + size and not two iterators

It appears that std::span in C++20 is defined similarly to template class span { T* begin; size_t count; }; And not template class span { Iter begin; Iter end; }; which is more general…
user877329
  • 6,717
  • 8
  • 46
  • 88
9
votes
2 answers

Why does std::span lack size_type?

I've been updating old code that used my homebrew span class to the one that is more in line with C++20 std::span and I'm getting compile errors because std::span doesn't have size_type and instead has index_type. There's been a huge drama over…
user3624760
8
votes
1 answer

why can't I construct an std::span from iterators?

Consider a large memory container. In this simple example an std::vector: std::vector v = { 0, 1, 2, 3, 4, 5 }; std::span allows me create a lightweight view over the memory. Now I want to simply print the span: template void…
Stack Danny
  • 7,754
  • 2
  • 26
  • 55
7
votes
1 answer

Create span of string_views from C string array

Given some function void func(std::span), how does one feed this function a raw array of C-strings const char** in the most efficient manner? As far as I understood this should be possible as without any copying as std::string_view…
Joel Bodenmann
  • 2,152
  • 2
  • 17
  • 44
6
votes
2 answers

How do you do bounds checking with std span?

std::vector and pretty much all other containers have a very convenient way of bounds checking: at(). std::span doesn't have that apparently. Why? Is there a replacement? Other than rolling out your own at()?
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
1
2 3 4