'std::span' Is Not a Member of 'std' Fix

Written November 28, 2026 · Tested with GCC 13.3.0 · 11 min read

By the end of this guide
  • The error: the include is missing
  • Neighboring headers: five fail, one rescues by accident
  • The fix
  • Reading through the view

Written November 28, 2026

Tested with GCC 13.3.0

What this guide is

std::span is a class template, since C++20, defined in the header <span>. It describes an object that can refer to a contiguous sequence of objects, with the first element of the sequence at position zero: a non-owning view over the sequence, not a container that owns the elements. A span can have a static extent, where the number of elements is known at compile time and encoded in the type, or a dynamic extent, where the count is carried at run time.

Because the class template lives in one specific header, and only exists from C++20, 'span' is not a member of 'std' has two common causes:

  • The #include <span> line is missing in a C++20 build. For this error GCC does name the header: its note says 'std::span' is defined in header '<span>'; did you forget to '#include <span>'?, and following that note is the whole fix.
  • The build is not in C++20 mode at all. Under -std=c++17 the same first error carries a different note — 'std::span' is only available from C++20 onwards — and adding the include does not fix that build. That case has its own section below.

One neighboring header does pull this name in by accident on this library, and one section below separates that accident from the rule.

The error: the include is missing

This program views an array of three int values through a span and reads its size and first element, but includes only <iostream>, compiled as C++20:

C++
#include <iostream>
int main() {
    int a[] = {10, 20, 30};
    std::span<int> s = a;
    std::cout << s.size() << "\n";
    std::cout << s[0] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 a_noinclude.cpp -o a_prog
a_noinclude.cpp: In function 'int main()':
a_noinclude.cpp:4:10: error: 'span' is not a member of 'std'
    4 |     std::span<int> s = a;
      |          ^~~~
a_noinclude.cpp:2:1: note: 'std::span' is defined in header '<span>'; did you forget to '#include <span>'?
    1 | #include <iostream>
  +++ |+#include <span>
    2 | int main() {
a_noinclude.cpp:4:15: error: expected primary-expression before 'int'
    4 |     std::span<int> s = a;
      |               ^~~
a_noinclude.cpp:5:18: error: 's' was not declared in this scope
    5 |     std::cout << s.size() << "\n";
      |                  ^

The first error is the defect. The primary-expression error that follows comes from the failed declaration — the compiler cannot read the template argument list of a name it does not know — and the undeclared-s error is a knock-on effect of the same failure. Fix the include and they all go.

Neighboring headers: five fail, one rescues by accident

Swapping in another nearby header mostly changes nothing here. Compiled one at a time as the only extra header besides <iostream>, in C++20 mode, each of these fails with the same first error, 'span' is not a member of 'std':

  • <array>
  • <vector>
  • <string_view>
  • <utility>
  • <memory>

That list includes the header for the other non-owning view, <string_view>, and headers for the contiguous containers a span most often views. None of them declares std::span on this libstdc++, not even transitively.

The exception is <ranges>: with <ranges> as the only extra header, the same program compiles and runs, printing 3. On this library, <ranges> pulls <span> in transitively. That is an implementation detail of this libstdc++, not a rule any program may rely on — another standard library, or another version of this one, is free to arrange its headers differently. Treat the <ranges> result as an explanation for code that compiles on one machine and fails on another, never as the fix: a program that uses std::span includes <span>.

The fix

Add the header, and keep the build in C++20 mode or later:

C++
#include <iostream>
#include <span>
int main() {
    int a[] = {10, 20, 30};
    std::span<int> s = a;
    std::cout << s.size() << "\n";
    std::cout << s[0] << "\n";
    std::cout << s[1] << "\n";
    std::cout << s[2] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 a_fixed.cpp -o a_fixed_prog
$ ./a_fixed_prog
3
10
20
30

One include, <span>, declares the class template. The span views the array without copying it: size() reports 3, and the subscript operator reads the three elements back in order, 10, 20, and 30.

Reading through the view

The same fixed shape reads the ends of the sequence and its raw storage. front() accesses the first element, back() accesses the last element, data() returns a pointer to the underlying contiguous storage, size_bytes() returns the size of the sequence in bytes, and empty() checks whether the sequence is empty:

C++
#include <iostream>
#include <span>
int main() {
    int a[] = {10, 20, 30};
    std::span<int> s = a;
    std::cout << s.size() << "\n";
    std::cout << s.front() << "\n";
    std::cout << s.back() << "\n";
    std::cout << s.data()[1] << "\n";
    std::cout << s.size_bytes() << "\n";
    std::cout << s.empty() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 b_fixed.cpp -o b_prog
$ ./b_prog
3
10
30
20
12
0

The output follows the calls in order: the size is 3, the first element is 10, the last is 30, the element reached through data() at index 1 is 20, the three int values occupy 12 bytes on this platform, and empty() reports 0 because the sequence is not empty.

Fixing the length in the type

The second template argument is the extent. Leaving it out gives a dynamic extent — the count is stored with the view at run time — while giving a number fixes the count at compile time and encodes it in the type. The static member extent reports which one a given span has:

C++
#include <iostream>
#include <span>
int main() {
    int a[] = {10, 20, 30};
    std::span<int, 3> s = a;
    std::cout << s.size() << "\n";
    std::cout << s.extent << "\n";
    std::cout << std::span<int>::extent << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 c_fixed_extent.cpp -o c_prog
$ ./c_prog
3
3
18446744073709551615

The fixed-extent span reports a size of 3 and an extent of 3. The dynamic span in the last line reports the constant std::dynamic_extent, which the <span> header defines as the largest value of std::size_t — printed here as 18446744073709551615. A dynamic extent is not a missing length; it is the marker that the length lives in the object instead of the type.

A fixed extent that does not match

Encoding the count in the type makes a mismatch a compile error. Asking for a fixed extent of 4 over an array of three elements fails, and the message is a conversion error, not the missing-member error this guide opened with:

C++
#include <iostream>
#include <span>
int main() {
    int a[] = {10, 20, 30};
    std::span<int, 4> s = a;
    std::cout << s.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 d_wrong_extent.cpp -o d_prog
d_wrong_extent.cpp: In function 'int main()':
d_wrong_extent.cpp:5:27: error: conversion from 'int [3]' to non-scalar type 'std::span<int, 4>' requested
    5 |     std::span<int, 4> s = a;
      |                           ^

That single error is the whole output. The include is right and the name is right; the count in the type disagrees with the array, so no conversion exists. Either correct the extent to match the sequence, or drop the extent and let the span take its size from the array at run time.

Cutting smaller views from a view

A span hands out smaller views of the same elements. first() obtains a subspan consisting of the first elements of the sequence, last() obtains one consisting of the last elements, and subspan() obtains a subview over consecutive elements chosen by an offset and a count. The free function std::as_bytes converts a span into a view of its underlying bytes:

C++
#include <iostream>
#include <span>
int main() {
    int a[] = {10, 20, 30, 40};
    std::span<int> s = a;
    std::cout << s.first(2).size() << "\n";
    std::cout << s.first(2).back() << "\n";
    std::cout << s.last(2).front() << "\n";
    std::cout << s.subspan(1, 2).front() << "\n";
    std::cout << s.subspan(1, 2).back() << "\n";
    std::cout << std::as_bytes(s).size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 e_subspan.cpp -o e_prog
$ ./e_prog
2
20
30
20
30
16

Read in order: the first two elements form a view of size 2 whose last element is 20; the last two elements start at 30; the two elements starting at offset 1 run from 20 to 30; and the byte view over four int values has 16 elements on this platform, one per byte. Every one of these views still points at the same array — cutting a view copies no elements.

Same first error, different cause: the build is in C++17 mode

The first error line alone does not say which cause is at work — the note under it does. Take the fixed program, with <span> included, and compile it as C++17 instead of C++20:

C++
#include <iostream>
#include <span>
int main() {
    int a[] = {10, 20, 30};
    std::span<int> s = a;
    std::cout << s.size() << "\n";
    std::cout << s[0] << "\n";
    std::cout << s[1] << "\n";
    std::cout << s[2] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 a_fixed.cpp -o x17f_prog
a_fixed.cpp: In function 'int main()':
a_fixed.cpp:5:10: error: 'span' is not a member of 'std'
    5 |     std::span<int> s = a;
      |          ^~~~
a_fixed.cpp:5:10: note: 'std::span' is only available from C++20 onwards
a_fixed.cpp:5:15: error: expected primary-expression before 'int'
    5 |     std::span<int> s = a;
      |               ^~~
a_fixed.cpp:6:18: error: 's' was not declared in this scope
    6 |     std::cout << s.size() << "\n";
      |                  ^

The include is present and the error is word-for-word the same, but the note names the real defect: this class template exists only from C++20 onwards, so no include fixes a C++17 build. The same failure, with the same note, occurs under -std=gnu++17. Switch the build to -std=c++20 (or a later standard mode). Read the note before editing the include list — the two causes share a first line and need different fixes.

A different wording: the name without its qualifier

Including <span> is not the only half of the name. This program includes the header but drops the std:: qualifier:

C++
#include <iostream>
#include <span>
int main() {
    int a[] = {10, 20, 30};
    span<int> s = a;
    std::cout << s.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 g_unqualified.cpp -o g_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:5: error: 'span' was not declared in this scope; did you mean 'std::span'?
    5 |     span<int> s = a;
      |     ^~~~
      |     std::span
In file included from g_unqualified.cpp:2:
/usr/include/c++/13/span:56:11: note: 'std::span' declared here
   56 |     class span;
      |           ^~~~
g_unqualified.cpp:5:10: error: expected primary-expression before 'int'
    5 |     span<int> s = a;
      |          ^~~
g_unqualified.cpp:6:18: error: 's' was not declared in this scope
    6 |     std::cout << s.size() << "\n";
      |                  ^

That wording — was not declared in this scope — is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::span; adding includes does not bring the name out of namespace std.

The exact rule

  • std::span is declared by #include <span>, and that is the only header required to declare it. <array>, <vector>, <string_view>, <utility>, and <memory> do not declare it, on this library not even transitively.
  • <ranges> does declare it in practice on this libstdc++, transitively — the sweep program with only <ranges> added compiled and ran. That is an implementation detail, not the rule, and no program should be written to depend on it.
  • In a C++20 build, 'span' is not a member of 'std' in a program that includes only <iostream> beside the use means the <span> include is missing, and GCC's note names the header directly: 'std::span' is defined in header '<span>'. Follow the note.
  • In a C++17 build, the same first error means something else: its note reads 'std::span' is only available from C++20 onwards, and the program fails the same way with <span> included. That build needs C++20 mode, not an include.
  • std::dynamic_extent is declared by the same header: it is the constant, of type std::size_t, that signifies a span has dynamic extent, and it is the default value of the extent template parameter.
  • std::as_bytes and std::as_writable_bytes are also declared by <span>: they convert a span into a view of its underlying bytes.
  • 'span' was not declared in this scope for an unqualified span use is a different defect: qualify the name as std::span — the include alone does not bring the name out of namespace std.
  • The follow-on errors after a failed declaration — expected primary-expression before 'int' and 's' was not declared in this scope — are knock-on effects of the missing declaration, not separate defects.

The fixes, ranked

  • First, read the note under the error. If it names the header — 'std::span' is defined in header '<span>' — add that include to the file that names std::span. If it says the name is only available from C++20 onwards, fix the build mode instead.
  • Second, check the include list of the file that names std::span. If <span> is not there and the build is C++20 or later, add it — that is the fix in the common case.
  • Third, do not chase the follow-on errors. Fix the include, or the build mode, and recompile; the primary-expression and undeclared-s errors are consequences, not causes.
  • Fourth, do not substitute a neighboring header. <array>, <vector>, <string_view>, <utility>, and <memory> were each tried here and each fails. If the program compiles only because <ranges> happens to be included, add <span> anyway — the transitive rescue is not portable.
  • Fifth, if the error says was not declared in this scope instead, fix the name's qualifier to std::span as well.
  • Sixth, if the error is a conversion error naming a fixed extent, the include is not the problem: make the extent match the sequence, or leave the extent out.

Which fix for which cause

  • #include <span> missing, std::span used in a C++20 build with <iostream> present: the message is 'span' is not a member of 'std' and the note names <span> — add #include <span>.
  • Build in C++17 mode, with or without #include <span>: the same first message, but the note says 'std::span' is only available from C++20 onwards — switch the build to C++20 mode or later.
  • Only <array>, <vector>, <string_view>, <utility>, or <memory> included, in a C++20 build: same message, same header note — add #include <span>.
  • Only <ranges> included, in a C++20 build on this libstdc++: the program compiles, by transitive include — add #include <span> anyway, so the file states its own dependency.
  • span used without the std:: qualifier, <span> included: the message is 'span' was not declared in this scope — qualify the name as std::span.
  • A fixed extent in the type disagrees with the sequence: the message is a conversion error naming the two types — correct the extent, or drop it for a dynamic extent.

Checklist

  • Check the include list of the file that names the span, not just the project. The include must be in that file, or in a header that file includes directly by design.
  • Read the note under the error before editing anything. For the missing include, GCC names the header — 'std::span' is defined in header '<span>'; for a pre-C++20 build, the note names the language mode instead, and the two fixes are different.
  • Confirm the build is in C++20 mode or later. std::span does not exist in earlier modes, with or without its header.
  • Do not rely on a neighboring header pulling std::span in. Five of the six headers tried here do not, and the one that does — <ranges> — does so only as a transitive detail of this library.
  • One header per name the file uses: output comes from <iostream>, even in a program whose subject is the span.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 3, 10, 20, and 30 for the direct read, and 3, 10, 30, 20, 12, and 0 for the accessor sequence.
  • When a length is known at compile time and must not change, state it as the extent in the type; when it is only known at run time, leave the extent out and read it back with size().

Sources

  • cppreference, std::span — for std::span (since C++20) being a class template that describes an object referring to a contiguous sequence of objects with the first element at position zero, for a span having either a static extent encoded in the type or a dynamic extent, for the extent template parameter defaulting to std::dynamic_extent, and for the front, back, operator[], data, size, size_bytes, empty, first, last, and subspan members.
  • cppreference, Standard library header <span> — for <span> (C++20) declaring std::span as a non-owning view over a contiguous sequence of objects, the std::dynamic_extent constant signifying that the span has dynamic extent, and the std::as_bytes and std::as_writable_bytes function templates that convert a span into a view of its underlying bytes.
  • cppreference, std::span::subspan — for subspan obtaining a subview over consecutive elements of the span, determined by an element count and an offset, with the function-argument overload always returning a span of dynamic extent.

Keep Exploring