C++
'std::optional' Is Not a Member of 'std' Fix
std::optional is a class template declared in the header
Written November 27, 2026 · Tested with GCC 13.3.0 · 10 min read
- The error: the include is missing
- The same error through neighboring headers: <string> and <variant> are not enough
- The fix
- The empty case: valueor, emplace, and reset
Written November 27, 2026
Tested with GCC 13.3.0
What this guide is
std::optional is a class template defined in the header <optional>. It manages an optional contained value — a value that may or may not be present. At any given point an instance either contains a value or does not contain a value: a default-initialized optional contains none, assigning a value puts one in, and has_value reports which state the object is in.
Because the class template lives in one specific header, 'optional' is not a member of 'std' has one common cause:
- The
#include <optional>line is missing. For this error GCC does name the header: its note says'std::optional' is defined in header '<optional>'; did you forget to '#include <optional>'?, and following that note is the whole fix.
The headers that sit near optional-valued code — <iostream>, <string>, <variant> — do not rescue the program. Each one is tested below, and each fails the same way.
The error: the include is missing
This program declares a std::optional, assigns a value to it, and reads the value back, but includes only <iostream>:
#include <iostream>
int main() {
std::optional<int> x;
x = 42;
std::cout << x.value() << "\n";
return 0;
}
$ g++ -std=c++17 noinclude.cpp -o noinclude_prog
noinclude.cpp: In function 'int main()':
noinclude.cpp:3:10: error: 'optional' is not a member of 'std'
3 | std::optional<int> x;
| ^~~~~~~~
noinclude.cpp:2:1: note: 'std::optional' is defined in header '<optional>'; did you forget to '#include <optional>'?
1 | #include <iostream>
+++ |+#include <optional>
2 | int main() {
noinclude.cpp:3:19: error: expected primary-expression before 'int'
3 | std::optional<int> x;
| ^~~
noinclude.cpp:4:5: error: 'x' was not declared in this scope
4 | x = 42;
| ^
The two errors after the note are knock-on effects of the first one: because the declaration of std::optional was never seen, the compiler cannot parse the declaration of x, and the later use of x has nothing to refer to. The default compilation mode prints the identical error, so this shape is not a language-mode problem. Fix the first error — the missing include — and the other two disappear with it.
The same error through neighboring headers: <string> and <variant> are not enough
Code that returns optional values almost always already includes a string or variant header, and those includes feel close enough. They are not. This program adds <string> and <variant> to the program above, and it fails the same way:
#include <iostream>
#include <string>
#include <variant>
int main() {
std::optional<int> x;
x = 42;
std::cout << x.value() << "\n";
return 0;
}
$ g++ -std=c++17 combined.cpp -o combined_prog
combined.cpp: In function 'int main()':
combined.cpp:5:10: error: 'optional' is not a member of 'std'
5 | std::optional<int> x;
| ^~~~~~~~
combined.cpp:4:1: note: 'std::optional' is defined in header '<optional>'; did you forget to '#include <optional>'?
3 | #include <variant>
+++ |+#include <optional>
4 | int main() {
combined.cpp:5:19: error: expected primary-expression before 'int'
5 | std::optional<int> x;
| ^~~
combined.cpp:6:5: error: 'x' was not declared in this scope
6 | x = 42;
| ^
The same result holds for the other neighboring headers tested, each with the same error and the same note naming <optional>:
- With only
<iostream>included, the program fails with'optional' is not a member of 'std'. - With only
<string>and<iostream>included, the program fails with'optional' is not a member of 'std'. - With only
<vector>and<iostream>included, the program fails with'optional' is not a member of 'std'. - With only
<memory>and<iostream>included, the program fails with'optional' is not a member of 'std'. - With only
<utility>and<iostream>included, the program fails with'optional' is not a member of 'std'. - With only
<variant>and<iostream>included, the program fails with'optional' is not a member of 'std'. - With only
<algorithm>and<iostream>included, the program fails with'optional' is not a member of 'std'.
One of those is worth pausing on. <variant> declares the other C++17 wrapper type, std::variant, and <utility> is the general utility header std::optional is cataloged with — both still do not declare std::optional. No neighboring header rescues this program on GCC 13.3.0 — there is no transitive-include exception here. No standard header is required to declare std::optional on behalf of <optional>, and a program that compiled only because some other header happened to pull the declaration in as a side effect could break under a different library version. Include <optional> in every file that names std::optional.
The fix
Add the header:
#include <iostream>
#include <optional>
int main() {
std::optional<int> x;
x = 42;
std::cout << x.has_value() << "\n";
std::cout << x.value() << "\n";
std::cout << *x << "\n";
return 0;
}
$ g++ -std=c++17 good.cpp -o good_prog
$ ./good_prog
1
42
42
The same program also compiles and runs with no -std flag at all. After the assignment the optional contains a value, so has_value prints 1, and both checked access through value and dereferencing with operator* print the contained value, 42. One include, <optional>, declares the class template.
The empty case: value_or, emplace, and reset
An optional that has not been given a value contains nothing, and the empty-state operations are the other half of using the type. value_or returns the contained value if there is one and the value given to it otherwise, emplace constructs a contained value in place, and reset destroys any contained value:
#include <iostream>
#include <optional>
int main() {
std::optional<int> x;
std::cout << x.has_value() << "\n";
std::cout << x.value_or(7) << "\n";
x.emplace(9);
std::cout << x.has_value() << "\n";
std::cout << x.value() << "\n";
x.reset();
std::cout << x.has_value() << "\n";
std::cout << x.value_or(7) << "\n";
return 0;
}
$ g++ -std=c++17 empty.cpp -o empty_prog
$ ./empty_prog
0
7
1
9
0
7
The default-constructed optional starts empty: has_value prints 0 and value_or(7) falls back to 7. After emplace(9) the optional contains a value — has_value prints 1 and value prints 9. reset empties it again, so the last two lines repeat the first two: 0, then the fallback 7. Reading an empty optional with value instead of value_or is a different path — value is the checked access and reports the empty state as an error rather than substituting a fallback.
The neighboring error: std::variant fails the same way, in its own header
std::variant is the type-safe union, defined in its own header <variant>: it holds a value of one of its alternative types. Without that header, the failure is worded exactly like the std::optional case, with the same kind of note naming its own header:
#include <iostream>
int main() {
std::variant<int, double> v;
v = 3.5;
std::cout << std::get<double>(v) << "\n";
return 0;
}
$ g++ -std=c++17 variant_noinclude.cpp -o variant_noinclude_prog
variant_noinclude.cpp: In function 'int main()':
variant_noinclude.cpp:3:10: error: 'variant' is not a member of 'std'
3 | std::variant<int, double> v;
| ^~~~~~~
variant_noinclude.cpp:2:1: note: 'std::variant' is defined in header '<variant>'; did you forget to '#include <variant>'?
1 | #include <iostream>
+++ |+#include <variant>
2 | int main() {
variant_noinclude.cpp:3:18: error: expected primary-expression before 'int'
3 | std::variant<int, double> v;
| ^~~
variant_noinclude.cpp:4:5: error: 'v' was not declared in this scope
4 | v = 3.5;
| ^
The two wrapper types are neighbors, not substitutes: <variant> does not declare std::optional, as the header test above showed, and <optional> does not declare std::variant. With <variant> added, the same program stores and reads its value normally:
#include <iostream>
#include <variant>
int main() {
std::variant<int, double> v;
v = 3.5;
std::cout << std::get<double>(v) << "\n";
std::cout << v.index() << "\n";
return 0;
}
$ g++ -std=c++17 variant_good.cpp -o variant_good_prog
$ ./variant_good_prog
3.5
1
std::get<double> prints the stored value, 3.5, and index prints 1 — the zero-based position of the double alternative in the type list. If a value is one of several possible types, that is the std::variant case; if it is one type that may be absent, that is the std::optional case above — and each needs its own include.
A different wording: optional without the std::
Calling the class without its namespace qualifier is a different defect, and GCC words it differently:
#include <iostream>
#include <optional>
int main() {
optional<int> x;
x = 42;
std::cout << x.value() << "\n";
return 0;
}
$ g++ -std=c++17 unqualified.cpp -o unqualified_prog
unqualified.cpp: In function 'int main()':
unqualified.cpp:4:5: error: 'optional' was not declared in this scope; did you mean 'std::optional'?
4 | optional<int> x;
| ^~~~~~~~
| std::optional
In file included from unqualified.cpp:2:
/usr/include/c++/13/optional:72:11: note: 'std::optional' declared here
72 | class optional;
| ^~~~~~~~
unqualified.cpp:4:14: error: expected primary-expression before 'int'
4 | optional<int> x;
| ^~~
unqualified.cpp:5:5: error: 'x' was not declared in this scope
5 | x = 42;
| ^
'optional' was not declared in this scope is the 'has not been declared' family for this topic: an unqualified name that lookup cannot find at all. This program already includes <optional>, so the include is not the problem — the declaration it adds is std::optional, inside namespace std, and the use must be qualified as std::optional, or the file must bring the name in deliberately. GCC's error itself carries the suggestion — did you mean 'std::optional'? — and the note under it points at the declaration in <optional>. If your error says is not a member of 'std', the name was qualified and the include is the whole problem; if it says was not declared in this scope, check the qualifier first.
The exact rule
std::optionalis declared by#include <optional>, and nowhere else by rule.<iostream>,<string>,<vector>,<memory>,<utility>,<variant>, and<algorithm>do not declarestd::optional.'optional' is not a member of 'std'means the<optional>include is missing, and GCC's note names the header directly:'std::optional' is defined in header '<optional>'; did you forget to '#include <optional>'?Follow the note.- The
expected primary-expression before 'int'and'x' was not declared in this scopeerrors that follow are knock-on effects of the missing declaration, not separate defects. std::variantis declared by#include <variant>, not by<optional>: without<variant>its error is worded the same way with a note naming<variant>, and neither header substitutes for the other.'optional' was not declared in this scopefor an unqualifiedoptional(...)use is a different defect: qualify the name asstd::optional— the include alone does not bring the name out of namespacestd.
The fixes, ranked
- First, check the include list of the file that names
std::optional. If<optional>is not there, add it — that is the fix in the common case, and it is the fix GCC's own note names. - Second, do not chase the follow-on errors. Fix the include and recompile; the parse error and the undeclared-
xerror are consequences, not causes. - Third, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::optionalas well. - Fourth, if the failing name is
std::variantand its note names<variant>, add<variant>instead — the two C++17 wrapper types live in different headers.
Which fix for which cause
#include <optional>missing,std::optionalused with<iostream>present: the message is'optional' is not a member of 'std'— add#include <optional>.- Only
<string>,<vector>,<memory>,<utility>,<variant>, or<algorithm>included: same message, same note — add#include <optional>. std::variantused without<variant>: the message is'variant' is not a member of 'std'with a note naming<variant>— add#include <variant>.optional(...)used without thestd::qualifier: the message is'optional' was not declared in this scope— qualify the name asstd::optionaland include<optional>.
Checklist
- Check the include list of the file that names the optional, 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. For this error GCC names the header —
'std::optional' is defined in header '<optional>'— so take the fix from the note instead of guessing. - Do not rely on a neighboring header pulling
std::optionalin. None of<iostream>,<string>,<vector>,<memory>,<utility>,<variant>, or<algorithm>does on this toolchain, and no header is required to. - If the optional holds a
std::string, that name comes from<string>— one header per name the file uses, even when a program happens to compile without it. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
1,42, and42for a valued optional, and0,7,1,9,0, and7across the empty,emplace, andresetsequence. - If the value is one of several possible types rather than one type that may be absent,
std::variantis the neighboring type — different header,<variant>, and the same missing-include error shape when that header is missing.
Related guides
- 'std::map' Is Not a Member of 'std' Fix — the same missing-include shape for the sorted map in
<map>, whose note likewise names its header. - 'std::unique_ptr' Is Not a Member of 'std' Fix — the same missing-include shape for the smart pointer in
<memory>, with the same note naming the header. - C++23 std::expected Error Handling Pipelines — the wider tour of the wrapper types this optional belongs with, including
std::expected.
Sources
- cppreference,
std::optional— forstd::optionalbeing defined in<optional>, managing a value that may or may not be present, containing no value when default-initialized or afterresetis called, andhas_value,value,value_or,operator*,emplace, andresetbeing its observer and modifier operations. - cppreference, Standard library header
<optional>— for<optional>declaring thestd::optionalclass template. - cppreference,
std::variant— forstd::variantbeing defined in<variant>, representing a type-safe union that holds a value of one of its alternative types, andindexandstd::getbeing its access operations. - cppreference, Standard library header
<variant>— for<variant>declaring thestd::variantclass template.
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.