'std::expected' Is Not a Member of 'std' Fix
Written November 28, 2026 · Tested with GCC 13.3.0 · 9 min read
- The error: the include is missing
- Neighboring headers: none of them rescues it
- The fix
- Reading the value and the error back
Written November 28, 2026
Tested with GCC 13.3.0
What this guide is
std::expected is a class template, since C++23, defined in the header <expected>. It provides a way to represent either of two values: an expected value of type T, or an unexpected value of type E, and an expected object is never valueless — it always holds one of the two.
Because the class template lives in one specific header, and only exists from C++23, 'expected' is not a member of 'std' has two common causes:
- The
#include <expected>line is missing in a C++23 build. For this error GCC does name the header: its note says'std::expected' is defined in header '<expected>'; did you forget to '#include <expected>'?, and following that note is the whole fix. - The build is not in C++23 mode at all. Under
-std=c++20the same first error carries a different note —'std::expected' is only available from C++23 onwards— and adding the include does not fix that build. That case has its own section below.
Unlike some neighboring types, no nearby header rescues this one on this library. Each candidate is separated below.
The error: the include is missing
This program stores an int in an expected and reads it back, but includes only <iostream> and <string>, compiled as C++23:
#include <iostream>
#include <string>
int main() {
std::expected<int, std::string> r = 42;
std::cout << r.value() << "\n";
return 0;
}
$ g++ -std=c++23 a_noinclude.cpp -o a_prog
a_noinclude.cpp: In function 'int main()':
a_noinclude.cpp:4:10: error: 'expected' is not a member of 'std'
4 | std::expected<int, std::string> r = 42;
| ^~~~~~~~
a_noinclude.cpp:2:1: note: 'std::expected' is defined in header '<expected>'; did you forget to '#include <expected>'?
1 | #include <iostream>
+++ |+#include <expected>
2 | #include <string>
a_noinclude.cpp:4:19: error: expected primary-expression before 'int'
4 | std::expected<int, std::string> r = 42;
| ^~~
a_noinclude.cpp:5:18: error: 'r' was not declared in this scope
5 | std::cout << r.value() << "\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-r error is a knock-on effect of the same failure. Fix the include and they all go.
Neighboring headers: none of them rescues it
Swapping in another nearby header changes nothing here. Compiled one at a time as the only extra header besides <iostream> and <string>, in C++23 mode, each of these fails with the same first error, 'expected' is not a member of 'std':
<optional><variant><any><utility><string><memory><type_traits>
That list includes headers for the other vocabulary types — <optional>, <variant>, and <any> — and headers that many programs already pull in for pairs and smart pointers. None of them declares std::expected on this libstdc++, not even transitively. There is no accidental rescue to rely on, and no shortcut: a C++23 program that uses std::expected includes <expected>.
The fix
Add the header, and keep the build in C++23 mode:
#include <expected>
#include <iostream>
#include <string>
int main() {
std::expected<int, std::string> r = 42;
std::cout << r.value() << "\n";
return 0;
}
$ g++ -std=c++23 a_fixed.cpp -o a_fixed_prog
$ ./a_fixed_prog
42
One include, <expected>, declares the class template. value() returns the expected value — here the stored int, which prints as 42.
Reading the value and the error back
The same fixed shape handles the error side. has_value() checks whether the object contains an expected value, and the conversion to bool reports the same state. An object built from std::unexpected holds the unexpected value instead, and error() returns that unexpected value:
#include <expected>
#include <iostream>
#include <string>
int main() {
std::expected<int, std::string> ok = 42;
std::cout << ok.value() << "\n";
std::cout << ok.has_value() << "\n";
std::expected<int, std::string> bad = std::unexpected(std::string("fail"));
std::cout << bad.has_value() << "\n";
std::cout << static_cast<bool>(bad) << "\n";
std::cout << bad.error() << "\n";
return 0;
}
$ g++ -std=c++23 b_fixed.cpp -o b_prog
$ ./b_prog
42
1
0
0
fail
The output follows the two objects in order: the first holds a value, so value() prints 42 and has_value() reports 1; the second holds an error, so has_value() and the bool conversion both report 0, and error() prints the stored string, fail.
The neighboring error: the helper fails too
The helper class template is declared by the same header, so a missing <expected> takes it down too. std::unexpected represents an unexpected value stored in a std::expected, and an expected has constructors that take a std::unexpected and build an object holding an unexpected value. Used on its own in a program that includes only <iostream> and <string>, in C++23 mode, it fails like this:
#include <iostream>
#include <string>
int main() {
auto u = std::unexpected(std::string("fail"));
std::cout << u.error() << "\n";
return 0;
}
$ g++ -std=c++23 c_unexp_no.cpp -o c_prog
c_unexp_no.cpp: In function 'int main()':
c_unexp_no.cpp:4:19: error: 'unexpected' is not a member of 'std'
4 | auto u = std::unexpected(std::string("fail"));
| ^~~~~~~~~~
For this name GCC prints no note naming the header — the single error above is the whole output for this program. The fix is still the same include. With the header added, the helper and the class work in one program: the expected built from an unexpected reports 0 for has_value() and reads its error back as fail, and a standalone unexpected reads its stored value back as oops through its own error() member, which returns a reference to the stored value:
#include <expected>
#include <iostream>
#include <string>
int main() {
std::expected<int, std::string> r = std::unexpected(std::string("fail"));
std::cout << r.has_value() << "\n";
std::cout << r.error() << "\n";
auto u = std::unexpected(std::string("oops"));
std::cout << u.error() << "\n";
return 0;
}
$ g++ -std=c++23 d_fixed.cpp -o d_prog
$ ./d_prog
0
fail
oops
One added include declares the class template and the helper in this section.
Asking for a value that is not there
A separate failure waits after the program compiles. Calling value() on an expected that contains an unexpected value throws std::bad_expected_access, the exception type that value() throws in that case; the thrown object stores a copy of the unexpected value, and its error() member returns that stored value. Catching it prints its message and the stored error:
#include <expected>
#include <iostream>
#include <string>
int main() {
std::expected<int, std::string> r = std::unexpected(std::string("fail"));
try {
std::cout << r.value() << "\n";
} catch (const std::bad_expected_access<std::string>& e) {
std::cout << e.what() << "\n";
std::cout << e.error() << "\n";
}
return 0;
}
$ g++ -std=c++23 e_bad.cpp -o e_prog
$ ./e_prog
bad access to std::expected without expected value
fail
That is a runtime error, not a compile error: the include is right, the call is wrong for the state the object is in. Checking has_value(), or the bool conversion, before calling value() avoids the throw when the state is not known in advance.
Same first error, different cause: the build is in C++20 mode
The first error line alone does not say which cause is at work — the note under it does. Take the fixed program, with <expected> included, and compile it as C++20 instead of C++23:
#include <expected>
#include <iostream>
#include <string>
int main() {
std::expected<int, std::string> r = 42;
std::cout << r.value() << "\n";
return 0;
}
$ g++ -std=c++20 a_fixed.cpp -o x20f_prog
a_fixed.cpp: In function 'int main()':
a_fixed.cpp:5:10: error: 'expected' is not a member of 'std'
5 | std::expected<int, std::string> r = 42;
| ^~~~~~~~
a_fixed.cpp:5:10: note: 'std::expected' is only available from C++23 onwards
a_fixed.cpp:5:19: error: expected primary-expression before 'int'
5 | std::expected<int, std::string> r = 42;
| ^~~
a_fixed.cpp:6:18: error: 'r' was not declared in this scope
6 | std::cout << r.value() << "\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++23 onwards, so no include fixes a C++20 build. Switch the build to -std=c++23 (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 <expected> is not the only half of the name. This program includes the header but drops the std:: qualifier:
#include <expected>
#include <iostream>
#include <string>
int main() {
expected<int, std::string> r = 42;
std::cout << r.value() << "\n";
return 0;
}
$ g++ -std=c++23 g_unqualified.cpp -o g_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:5: error: 'expected' was not declared in this scope; did you mean 'std::expected'?
5 | expected<int, std::string> r = 42;
| ^~~~~~~~
| std::expected
In file included from g_unqualified.cpp:1:
/usr/include/c++/13/expected:60:11: note: 'std::expected' declared here
60 | class expected;
| ^~~~~~~~
g_unqualified.cpp:5:14: error: expected primary-expression before 'int'
5 | expected<int, std::string> r = 42;
| ^~~
g_unqualified.cpp:6:18: error: 'r' was not declared in this scope
6 | std::cout << r.value() << "\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::expected; adding includes does not bring the name out of namespace std.
The exact rule
std::expectedis declared by#include <expected>, and nowhere else by rule.<optional>,<variant>,<any>,<utility>,<string>,<memory>, and<type_traits>do not declare it, on this library not even transitively.- In a C++23 build,
'expected' is not a member of 'std'in a program that includes only<iostream>and<string>beside the use means the<expected>include is missing, and GCC's note names the header directly:'std::expected' is defined in header '<expected>'. Follow the note. - In a C++20 build, the same first error means something else: its note reads
'std::expected' is only available from C++23 onwards, and the program fails the same way with<expected>included. That build needs C++23 mode, not an include. std::unexpectedis declared by the same#include <expected>: in the program shown here, which includes only<iostream>and<string>beside the use and is compiled as C++23, it gets its ownis not a member of 'std'error, with no note naming a header.std::bad_expected_accessis also declared by<expected>: it is the exception thrown whenvalue()is called on anexpectedthat contains an unexpected value.'expected' was not declared in this scopefor an unqualifiedexpecteduse is a different defect: qualify the name asstd::expected— the include alone does not bring the name out of namespacestd.- The follow-on errors after a failed declaration —
expected primary-expression before 'int'and'r' 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::expected' is defined in header '<expected>'— add that include to the file that namesstd::expected. If it says the name is only available from C++23 onwards, fix the build mode instead. - Second, check the include list of the file that names
std::expected. If<expected>is not there and the build is C++23, 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-
rerrors are consequences, not causes. - Fourth, do not substitute a neighboring header.
<optional>,<variant>,<any>,<utility>,<memory>, and<type_traits>were each tried here and each fails — there is no transitive rescue to arrange instead. - Fifth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::expectedas well. - Sixth, if the failing name is the helper —
unexpected— the same added include fixes it: that name shares the header. - Seventh, if the program compiles but
value()throws at run time, the include is not the problem: checkhas_value()before asking for the value.
Which fix for which cause
#include <expected>missing,std::expectedused in a C++23 build with<iostream>and<string>present: the message is'expected' is not a member of 'std'and the note names<expected>— add#include <expected>.- Build in C++20 mode, with or without
#include <expected>: the same first message, but the note says'std::expected' is only available from C++23 onwards— switch the build to C++23 mode. - Only
<optional>,<variant>,<any>,<utility>,<memory>, or<type_traits>included, in a C++23 build: same message, same header note — add#include <expected>. std::unexpectedused without<expected>in a C++23 build: its ownis not a member of 'std'error, with no note naming a header — add#include <expected>.expectedused without thestd::qualifier,<expected>included: the message is'expected' was not declared in this scope— qualify the name asstd::expected.value()called on anexpectedthat holds an unexpected value: the program throwsstd::bad_expected_accessat run time — check the state before asking for the value.
Checklist
- Check the include list of the file that names the
expected, 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::expected' is defined in header '<expected>'; for a pre-C++23 build, the note names the language mode instead, and the two fixes are different. - Confirm the build is in C++23 mode or later.
std::expecteddoes not exist in earlier modes, with or without its header. - Do not rely on a neighboring header pulling
std::expectedin. None of the seven headers tried here does, and no header except<expected>is required to. - One header per name the file uses:
std::stringcomes from<string>and output from<iostream>, even in a program whose subject is theexpected. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
42for the direct read, and42,1,0,0, andfailfor the value-and-error sequence. - When the state is not known, check
has_value()before callingvalue()— the call throwsstd::bad_expected_accesswhen the object holds an unexpected value instead.
Related guides
- 'std::optional' Is Not a Member of 'std' Fix — the same missing-include shape for the vocabulary type in
<optional>, one of the headers that does not rescue theexpectedhere. - 'std::variant' Is Not a Member of 'std' Fix — the same missing-include shape for the type-safe union in
<variant>, one of the headers that does not rescue theexpectedhere. - C++23 std::expected Error-Handling Pipelines — what to build with
std::expectedonce it compiles: monadic chains and error-type design, a different subject from this missing-include error.
Sources
- cppreference,
std::expected— forstd::expectedbeing a class template (since C++23) defined in<expected>that provides a way to represent either an expected value of typeTor an unexpected value of typeE, is never valueless, and for thevalue,error,has_value, andoperator boolobservers. - cppreference, Standard library header
<expected>— for<expected>(C++23) declaringstd::expected,std::unexpected, andstd::bad_expected_accessas class templates, and thestd::unexpecttag. - cppreference,
std::unexpected— forstd::unexpected(since C++23) representing an unexpected value stored in astd::expected, forstd::expectedhaving constructors with astd::unexpectedas a single argument that create an object containing an unexpected value, and for theerror()member returning a reference to the stored value. - cppreference,
std::bad_expected_access— forstd::bad_expected_access(since C++23) being defined in<expected>, as the type of object thrown bystd::expected::valuewhen accessing an expected object that contains an unexpected value, storing a copy of the unexpected value, and for itserror()andwhat()members returning the stored value and the explanatory string.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.