Questions tagged [std-span]

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

std-span
Filter by
Sorted by
Tagged with
11 votes
1 answer
3k views

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 ...
1 vote
0 answers
68 views

What is the modern C++ way of handling multi-channel data using span?

I have learned that since std::<span>'s introduction in C++20, we no longer need to use the traditional void read(T* input, int numSamples) API and can instead pass in a span that encompasses ...
6 votes
3 answers
2k views

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 ...
13 votes
4 answers
8k views

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

I wanted to do this #include <vector> #include <span> struct S { std::vector<int> v; void set(std::span<int> _v) { v = _v; } }; But it does not ...
1 vote
1 answer
28 views

What should I call this unique-pointer-with-size structure?

I'm annoyed by how C++' standard library only offers some functionality via pointers rather than structures which keep both an address and a size. Specifically, suppose I want something like a ...
5 votes
2 answers
522 views

What's the purpose of std::dynamic_extent in std::span

I know std::span is static. It is just view over bunch of vector/array/etc. elements. I see constructors of span, and it seems like std::dynamic_extent is used in 4-6. But in those constructors, there ...
1 vote
1 answer
61 views

Instantiating a function template with a std::span<T> parameter

I'm encountering an issue where the compiler seems unable to implicitly deduce the template argument T when I try to call largest() with a std::vector<T>. Explicitly specifying T (e.g., largest&...
3 votes
0 answers
117 views

Is a range-based loop over a default constructed std::span undefined behavior? [duplicate]

I am wondering whether a range based loop over a default constructed std::span is defined behavior since I am conditionally assigning to a default constructed std::span before looping. For example can ...
0 votes
0 answers
47 views

Make function template of span<auto> accept collections with inferrence of element type at call sites

I'd like to be able to pass values of as wide as possible a range of types to functions based on a template accepting span. I've got the following code: #include <iostream> #include <span> ...
440 votes
4 answers
180k views

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

Recently I've gotten suggestions to use span<T>'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 ...
2 votes
1 answer
111 views

Is it possible to recover the actual index of elements in a `std::span`?

I have a class with a std::vector member variable. I also have a member function that takes a contiguous range in that std::vector, specified by two size_t parameters, and performs some operations on ...
5 votes
1 answer
319 views

How to convert `const std::vector<T*>` into `std::span<const T*>`

Title. I am implementing this class: #include <span> #include <vector> class MyClass { public: std::span<int *> numbers(void) { return m_numbers; } std::span<...
13 votes
3 answers
9k views

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 ...
1 vote
1 answer
227 views

Can't construct std::span<int> from temporary std::array<int>

I have the following code, which I expected to work, but it doesn't: #include <array> #include <span> void foo(std::span<int>); int main() { foo(std::array<int, 3>{}); } ...
11 votes
3 answers
472 views

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

In the following C++20 code, passing a std::vector to a templated function with a std::span<T> parameter fails, because obviously the compiler cannot deduce the template parameter. I have tried ...
0 votes
1 answer
111 views

Why does `std::span` require an explicit constructor to use in a templated function? [duplicate]

Godbolt link. I have the following function: #include <algorithm> #include <iostream> #include <numeric> #include <ranges> #include <span> #include <type_traits> ...
2 votes
2 answers
2k views

How to use span to wrap up command line args

Is this use of the upcoming C++20 std::span correct and with no overhead to wrap up the command line arguments? #include <iostream> #include <span> int main(int argc, const char* argv[]) ...
5 votes
3 answers
373 views

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<uint8_t,N> from a std::span<uint8_t,N> but I cannot find a way to do so without memcpy, std::copy, or std::ranges::copy which don't protect me against ...
16 votes
1 answer
477 views

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<int> v = {1, 2, 3}; std::vector<int> w = {4, 5, 6}; std::...
1 vote
1 answer
250 views

Why do I need to pass std::span for a string argument when using std::thread in C++?

I've written the following code, which creates a thread that repeatedly prints a string. In this code, I can directly pass a string as an argument to the repeat() function in the main() function. ...
4 votes
1 answer
431 views

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

The converting constructor of C++ std::span of the form: template<class OtherElementType, size_t OtherExtent> constexpr explicit(see below) span( const span<OtherElementType, ...
4 votes
2 answers
142 views

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<size_t N> constexpr span( type_identity_t<element_type> (&arr)[N]) noexcept; Why is type_identity_t ...
1 vote
1 answer
318 views

What is the difference between the two overloads of std::span::subspan()

It appears that std::span<T,Extent>::subspan has two overloads. Here (live): #include <array> #include <span> #include <fmt/core.h> void show_sizes( const std::span<const ...
0 votes
2 answers
149 views

adding member function substr for std::span<T> which imitates the string_view::substr

In my code, I would like to create a new class named Span by extending the std::span class, but the new Span has some extra member functions which does not exist in std::span. One is adding the ...
1 vote
2 answers
125 views

template argument deduce error for raw C string literal but works for std::string_view

I'm designing a PEG parser in C++, and the parser should support both std::string_view and std::span<Token> as the token stream input. In the code, I see that one template class can only be ...
3 votes
2 answers
759 views

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

when reading the document of std::span, I see there is no method to remove the first element from the std::span<T>. Can you suggest a way to solve my issue? The large picture of my problem(I ...
1 vote
2 answers
363 views

How to construct a span from std::iota?

The following works: #include <vector> #include <ranges> int main() { auto view = std::vector<int>{0,1,2,3,4}; auto s = std::span{view.begin(), view.end()}; std::vector ...
3 votes
3 answers
3k views

How to make span of spans

C++20 std::span is a very nice interface to program against. But there doesn't seem to be an easy way to have a span of spans. Here's what I am trying to do: #include <iostream> #include <...
1 vote
1 answer
2k views

Emscripten and C++ 20

It looks like emscripten does not support C++ 20 I try to compile this: #include <stdio.h> #include <span> using std::span; int main() { int a[2] = {1, 3}; printf("hello, ...
0 votes
0 answers
68 views

Custom std::vector wrapper with view support

I have a large codebase that uses std::vector as data storage for numbers. However, now I need to add support for raw data views, due to external C library that returns raw array. Due to performance ...
1 vote
1 answer
545 views

Why can logical constness only be added to a std::span of const pointers?

Consider this code that attempts to create various std::span objects for a vector of raw pointers. #include <vector> #include <span> int main() { struct S {}; std::vector<S*>...
3 votes
1 answer
641 views

What's the best way to pass a r-value/temporary collection to a function taking a std::span?

I've been trying to start using std::span<const T> in places where I would have previously used const std::vector<T>&. The only sticking point I have is illustrated in the following: #...
14 votes
1 answer
720 views

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 ...
4 votes
1 answer
4k views

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 ...
3 votes
1 answer
1k views

How to initialize std::span<const T*>? Problems with const-ness

I am trying to initialize a span<const T*> — that is, a list of pointers to const data. However, the rules for const conversion amongst pointers and span<>'s available constructors are ...
3 votes
1 answer
2k views

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 ...
0 votes
1 answer
2k views

How to pass an empty span object?

Is there a way to pass an empty std::span<int> to a function? I have a function like below: bool func( const std::vector<int>& indices ) { if ( !indices.empty( ) ) { /* ...
2 votes
1 answer
2k views

Where to use std::span?

I want to write a function that can accept any type of contiguous buffer (e.g. std::array, std::vector, raw array, etc) from its call site. I have come up with two methods. Method #1: void func( int* ...
4 votes
1 answer
921 views

Is there a common pattern for passing vector<string> as span<string_view>?

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<std::string_view>? Any good overall pattern to ...
3 votes
2 answers
1k views

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<typename T> class SimpleSpan { T* ptr; ...
2 votes
1 answer
974 views

How to make class compatible with std::span constructor that takes a range?

I'd like to be able to pass my custom container this std::span constructor: template< class R > explicit(extent != std::dynamic_extent) constexpr span( R&& range ); What do I need to ...
3 votes
1 answer
443 views

Cannot satisfy C++ range requirements custom container

I have been trying to write a custom container with its own iterator which can be used as a range and with std::span. I am new to ranges, so please be kind. The following example fails to compile, ...
5 votes
1 answer
648 views

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::...
6 votes
1 answer
771 views

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

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 ...
5 votes
2 answers
3k views

Is it possible to construct a `std::span` from a view in C++20?

This example program does not compile, because the transform_view cannot be converted to a std::span: class Foo { private: std::vector<std::string> strings = { "a", "b", &...
1 vote
2 answers
753 views

How to use user defined concept as a template type of std::span?

I would like to use my user defined concept as a template type of std::span but template argument deduction does not work as I expected. When I try to pass a "std::array of char" to template ...
4 votes
4 answers
1k views

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 <span> template <typename T1, typename T2> bool isIn(const T1& x, std::span<const T2> v) {...
0 votes
0 answers
128 views

Should std::ssize() still needed in C++20? [duplicate]

This answer answer that why C++20 introduced the std::ssize(), but according to latest std::span's specification, the size_type of std::span is now unsigned std::size_t, so is there other use case for ...
10 votes
1 answer
3k views

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

Consider a large memory container. In this simple example an std::vector<int>: std::vector v = { 0, 1, 2, 3, 4, 5 }; std::span allows me create a lightweight view over the memory. Now I want to ...
7 votes
1 answer
2k views

Create span of string_views from C string array

Given some function void func(std::span<std::string_view>), 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 ...