A non-owning lightweight wrapper object referring to a contiguous sequence of elements in memory
Questions tagged [std-span]
60 questions
3
votes
1 answer
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, because my container class cannot be converted to a…

Phil Rosenberg
- 1,597
- 1
- 14
- 22
3
votes
3 answers
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
#include
#include
#include
void…

Aykhan Hagverdili
- 28,141
- 6
- 41
- 93
2
votes
2 answers
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 is a required template parameter for the size -…

code muncher
- 1,592
- 2
- 27
- 46
2
votes
1 answer
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* const buffer, const std::size_t…

digito_evo
- 3,216
- 2
- 14
- 42
2
votes
1 answer
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 add to my custom class to make it satisfy the requirements to be…

Erunehtar
- 1,583
- 1
- 19
- 38
2
votes
2 answers
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 strings = { "a", "b", "c" };
public:
std::span getStrings() {
return…

Carsten
- 11,287
- 7
- 39
- 62
2
votes
2 answers
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
#include
int main(int argc, const char* argv[])
{
for (auto s : std::span { argv,…

Nico Engels
- 407
- 2
- 12
2
votes
4 answers
What's an idiomatic way to refer to either std::whatever or not_yet_in_std::whatever?
I like spans, so I use gsl::span here and there. But - in C++20, it's going to be std::span instead*. I use std::optional, but for C++14 code, it needs to be std::experimental::optional. And so on.
What's an idiomatic and sort-of future-proof way to…

einpoklum
- 118,144
- 57
- 340
- 684
2
votes
1 answer
Is it a good idea to base a non-owning bit container on std::vector? std::span?
In a couple of projects of mine I have had an increasing need to deal with contiguous sequences of bits in memory - efficiently (*). So far I've written a bunch of inline-able standalone functions, templated on the choice of a "bit container" type…

einpoklum
- 118,144
- 57
- 340
- 684
1
vote
1 answer
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. However, when I want to create a thread that calls…

Sami
- 513
- 4
- 11
1
vote
1 answer
Can't construct std::span from temporary std::array
I have the following code, which I expected to work, but it doesn't:
#include
#include
auto f = std::span(std::array{});
clang fails to compile this:
error: no matching conversion for functional-style cast from…

Jan Schultke
- 17,446
- 6
- 47
- 96
1
vote
1 answer
What is the difference between the two overloads of std::span::subspan()
It appears that std::span::subspan has two overloads.
Here (live):
#include
#include
#include
void show_sizes( const std::span command_line_arguments )
{
fmt::print( "{}\n",…

digito_evo
- 3,216
- 2
- 14
- 42
1
vote
2 answers
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 as the token stream input.
In the code, I see that one template class can only be instantiated by some code snippet like auto p2 =…

ollydbg23
- 1,124
- 1
- 12
- 38
1
vote
2 answers
How to construct a span from std::iota?
The following works:
#include
#include
int main() {
auto view = std::vector{0,1,2,3,4};
auto s = std::span{view.begin(), view.end()};
std::vector test(view.begin(), view.end());
}
But this does not:
#include…

bradgonesurfing
- 30,949
- 17
- 114
- 217
1
vote
1 answer
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
#include
int main()
{
struct S {};
std::vector v;
std::span span1{v};
std::span span2{v};
…

MarkB
- 672
- 2
- 9