C++

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

std::optional is a class template declared in the header , and the headers around optional-valued code do not declare it — not , not , and not . GCC helps here: its note names directly. The fix is to add the include the note points at.

Written November 27, 2026 · Tested with GCC 13.3.0 · 10 min read

By the end of this guide
  • 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>:

C++
#include <iostream>
int main() {
    std::optional<int> x;
    x = 42;
    std::cout << x.value() << "\n";
    return 0;
}
Terminal
$ 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:

C++
#include <iostream>
#include <string>
#include <variant>
int main() {
    std::optional<int> x;
    x = 42;
    std::cout << x.value() << "\n";
    return 0;
}
Terminal
$ 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:

C++
#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;
}
Terminal
$ 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:

C++
#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;
}
Terminal
$ 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:

C++
#include <iostream>
int main() {
    std::variant<int, double> v;
    v = 3.5;
    std::cout << std::get<double>(v) << "\n";
    return 0;
}
Terminal
$ 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:

C++
#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;
}
Terminal
$ 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:

C++
#include <iostream>
#include <optional>
int main() {
    optional<int> x;
    x = 42;
    std::cout << x.value() << "\n";
    return 0;
}
Terminal
$ 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::optional is declared by #include <optional>, and nowhere else by rule. <iostream>, <string>, <vector>, <memory>, <utility>, <variant>, and <algorithm> do not declare std::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 scope errors that follow are knock-on effects of the missing declaration, not separate defects.
  • std::variant is 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 scope for an unqualified optional(...) use is a different defect: qualify the name as std::optional — the include alone does not bring the name out of namespace std.

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-x error are consequences, not causes.
  • Third, if the error says was not declared in this scope instead, fix the name's qualifier to std::optional as well.
  • Fourth, if the failing name is std::variant and 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::optional used 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::variant used without <variant>: the message is 'variant' is not a member of 'std' with a note naming <variant> — add #include <variant>.
  • optional(...) used without the std:: qualifier: the message is 'optional' was not declared in this scope — qualify the name as std::optional and 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::optional in. 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, and 42 for a valued optional, and 0, 7, 1, 9, 0, and 7 across the empty, emplace, and reset sequence.
  • If the value is one of several possible types rather than one type that may be absent, std::variant is the neighboring type — different header, <variant>, and the same missing-include error shape when that header is missing.

Sources

Keep Exploring