Questions tagged [std-span]

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

60 questions
5
votes
3 answers

Why can T not be deduced for std::span when passing a std::vector?

In the following C++20 code, passing a std::vector to a templated function with a std::span parameter fails, because obviously the compiler cannot deduce the template parameter. I have tried this with GCC, Clang and MSVC; all fail. Invoking like…
FrankM
  • 1,007
  • 6
  • 15
5
votes
1 answer

Why C++20 knows how to hash string_view, but does not know how to hash span?

My best guesses are that committee either forgot about this use case or did not want to use concepts/requires to restrict the span type to something that can be safely hashed(POD, no padding), or they did not want half solutions(waiting for…
NoSenseEtAl
  • 28,205
  • 28
  • 128
  • 277
5
votes
2 answers

Convert a nullptr to std::span

I have a const z* zs = nullptr; I want to convert zs to std::span When I try to do std::span(zs) I get an error saying error: no matching function for call to ‘std::span::span(const z* const&)’ How do I convert to zs to…
4
votes
1 answer

Create std::string from std::span of unsigned char

I am using a C library which uses various fixed-sized unsigned char arrays with no null terminator as strings. I've been converting them to std::string using the following function: auto uchar_to_stdstring(const unsigned char* input_array, int…
Simog
  • 193
  • 3
  • 13
4
votes
1 answer

Can std::span iterators outlive the span object they are created from?

Put it other way, conversely, are std::span iterators invalidated after the span instance is destroyed? I have a vector I need to iterate over with different layouts. I'm trying to make use of std::span to avoid writing a lot of iterator boilerplate…
ofo
  • 1,160
  • 10
  • 21
4
votes
4 answers

Generic function using std::span doesn't compile

I wanted to make an isIn function that takes an std::span. This is my attempt: #include template bool isIn(const T1& x, std::span v) { for (const T2& e : v) if (e == x) return…
tuket
  • 3,232
  • 1
  • 26
  • 41
3
votes
3 answers

What is the idiomatic way to create a fixed size std::array from a fixed size std::span?

I am trying to create a std::array from a std::span but I cannot find a way to do so without memcpy, std::copy, or std::ranges::copy which don't protect me against wrong specification of destination array size. #include…
phinz
  • 1,225
  • 10
  • 21
3
votes
1 answer

C++20 std::span: Uses of converting constructor

The converting constructor of C++ std::span of the form: template constexpr explicit(see below) span( const span& s) noexcept; with constraints, can be used for the…
cbhattac
  • 121
  • 4
3
votes
1 answer

Why does C++ span's C style array constructor need type_identity_t?

The C style array constructor for span is specified as follows template constexpr span( type_identity_t (&arr)[N]) noexcept; Why is type_identity_t necessary? instead of just: template constexpr span( …
cbhattac
  • 121
  • 4
3
votes
1 answer

What is an mdspan, and what is it used for?

Over the past year or so I've noticed a few C++-related answers on StackOverflow refer to mdspan's - but I've never actually seen these in C++ code. I tried looking for them in my C++ compiler's standard library directory and in the C++ coding…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
3
votes
2 answers

what is the way to remove the first element from a std::span?

when reading the document of std::span, I see there is no method to remove the first element from the std::span. Can you suggest a way to solve my issue? The large picture of my problem(I asked in another question: How to instantiatiate a…
ollydbg23
  • 1,124
  • 1
  • 12
  • 38
3
votes
1 answer

How to initialize std::span? Problems with const-ness

I am trying to initialize a span — that is, a list of pointers to const data. However, the rules for const conversion amongst pointers and span<>'s available constructors are thwarting me. I'm sure there's a way to do it, but I cannot find…
jwd
  • 10,837
  • 3
  • 43
  • 67
3
votes
1 answer

Is std::span a view?

Is std::span a view? My speculation stems from the fact that it does not "own". I have read that it is a reference to ranges here and that it "just wraps" I have not seen anywhere been stated explicitly that it is a view.
gonidelis
  • 885
  • 10
  • 32
3
votes
1 answer

Is there a common pattern for passing vector as span?

I don't think there's any getting away from the need to allocate a buffer to hold the span of string_views. So probably an intermediate std::vector? Any good overall pattern to crush it down to 1 line? If necessary, what kind of…
VoidStar
  • 5,241
  • 1
  • 31
  • 45
3
votes
2 answers

Is using std::span into an std::vector after push_back is called undefined behavior

My understanding of std::span is that it is essentially contains pointer into a container, a size, and some useful member functions. template class SimpleSpan { T* ptr; size_t length; // some member functions } I can take a…