'std::variant' Is Not a Member of 'std' Fix
Written November 28, 2026 · Tested with GCC 13.3.0 · 11 min read
- The error: the include is missing
- Neighboring headers: none of them rescues it
- The fix
- Reading the value back
Written November 28, 2026
Tested with GCC 13.3.0
What this guide is
std::variant is a class template, since C++17, defined in the header <variant>. It represents a type-safe union: an instance holds a value of one of its alternative types at any given time, or, in the case of error, no value. A default-constructed instance holds a value of its first alternative, unless that alternative is not default-constructible.
Because the template lives in one specific header, 'variant' is not a member of 'std' has one common cause:
- The
#include <variant>line is missing. For this error GCC does name the header: its note says'std::variant' is defined in header '<variant>'; did you forget to '#include <variant>'?, and following that note is the whole fix.
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 a variant that can also hold a string, and reads it back, but includes only <iostream> and <string>:
#include <iostream>
#include <string>
int main() {
std::variant<int, std::string> v = 42;
std::cout << std::get<int>(v) << "\n";
return 0;
}
$ g++ -std=c++20 a_noinclude.cpp -o a_prog
a_noinclude.cpp: In function 'int main()':
a_noinclude.cpp:4:10: error: 'variant' is not a member of 'std'
4 | std::variant<int, std::string> v = 42;
| ^~~~~~~
a_noinclude.cpp:2:1: note: 'std::variant' is defined in header '<variant>'; did you forget to '#include <variant>'?
1 | #include <iostream>
+++ |+#include <variant>
2 | #include <string>
a_noinclude.cpp:4:18: error: expected primary-expression before 'int'
4 | std::variant<int, std::string> v = 42;
| ^~~
a_noinclude.cpp:5:32: error: 'v' was not declared in this scope
5 | std::cout << std::get<int>(v) << "\n";
| ^
The first error is the defect. The errors that follow are knock-on effects: the declaration of v failed, so the later use of v fails too. 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>, each of these fails with the same first error, 'variant' is not a member of 'std':
<string><optional><any><tuple><utility><memory><type_traits>
That list includes headers for the other vocabulary types — <optional> and <any> — and headers that many programs already pull in for pairs, tuples, and smart pointers. None of them declares std::variant on this libstdc++, not even transitively. There is no accidental rescue to rely on, and no shortcut: a program that uses std::variant includes <variant>.
The fix
Add the header:
#include <iostream>
#include <string>
#include <variant>
int main() {
std::variant<int, std::string> v = 42;
std::cout << std::get<int>(v) << "\n";
return 0;
}
$ g++ -std=c++20 a_fixed.cpp -o a_fixed_prog
$ ./a_fixed_prog
42
One include, <variant>, declares the template. std::get<int> reads the value back by type — it returns a reference to the stored value when the variant holds that type, and throws on error when it does not.
Reading the value back
The same fixed shape handles both alternatives. Assign a string, read it back with get, and ask for the position of the held alternative with the index member, which returns the zero-based index of the alternative currently held:
#include <iostream>
#include <string>
#include <variant>
int main() {
std::variant<int, std::string> v = 42;
std::cout << std::get<int>(v) << "\n";
v = std::string("hi");
std::cout << std::get<std::string>(v) << "\n";
std::cout << v.index() << "\n";
return 0;
}
$ g++ -std=c++20 b_fixed.cpp -o b_prog
$ ./b_prog
42
hi
1
The output follows the assignments in order: the int prints as 42, the string prints as hi, and index reports 1 because the string is the second alternative, counting from zero.
Visiting the value
Reading by type works when the caller already knows which alternative is held. std::visit covers the other case: it calls the given callable with the value currently held, whichever alternative that is. With an overloaded helper, each alternative gets its own function, and a generic lambda handles any alternative in one body:
#include <iostream>
#include <string>
#include <variant>
template<class... Ts>
struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
int main() {
std::variant<int, std::string> v = 42;
auto show = overloaded{
[](int x) { std::cout << "int " << x << "\n"; },
[](const std::string& s) { std::cout << "string " << s << "\n"; }
};
std::visit(show, v);
v = std::string("hi");
std::visit(show, v);
std::visit([](auto&& x) { std::cout << x << "\n"; }, v);
return 0;
}
$ g++ -std=c++20 f_realistic.cpp -o f_prog
$ ./f_prog
int 42
string hi
hi
The overloaded helper prints a label per type — int 42, then string hi after the assignment — and the generic lambda prints the held string plainly as hi. The visitor must be callable with every alternative; that is what makes the call exhaustive by construction.
The neighboring errors: the helpers fail too
The helper functions are declared by the same header, so a missing <variant> takes them down with the template itself. This program calls std::visit without the header:
#include <iostream>
#include <string>
int main() {
std::variant<int, std::string> v = 42;
std::visit([](auto&& x) { std::cout << x << "\n"; }, v);
return 0;
}
$ g++ -std=c++20 c_visit_no.cpp -o c_prog
c_visit_no.cpp: In function 'int main()':
c_visit_no.cpp:4:10: error: 'variant' is not a member of 'std'
4 | std::variant<int, std::string> v = 42;
| ^~~~~~~
c_visit_no.cpp:2:1: note: 'std::variant' is defined in header '<variant>'; did you forget to '#include <variant>'?
1 | #include <iostream>
+++ |+#include <variant>
2 | #include <string>
c_visit_no.cpp:4:18: error: expected primary-expression before 'int'
4 | std::variant<int, std::string> v = 42;
| ^~~
c_visit_no.cpp:5:10: error: 'visit' is not a member of 'std'
5 | std::visit([](auto&& x) { std::cout << x << "\n"; }, v);
| ^~~~~
c_visit_no.cpp:5:10: note: 'std::visit' is defined in header '<variant>'; did you forget to '#include <variant>'?
c_visit_no.cpp:5:58: error: 'v' was not declared in this scope
5 | std::visit([](auto&& x) { std::cout << x << "\n"; }, v);
| ^
The variant error surfaces first, because the object is declared on the earlier line, and then visit gets its own error with its own note naming the same header. std::holds_alternative — which checks whether a variant currently holds a given type, and returns true or false — behaves the same way:
#include <iostream>
#include <string>
int main() {
std::variant<int, std::string> v = 42;
std::cout << std::holds_alternative<int>(v) << "\n";
return 0;
}
$ g++ -std=c++20 c_holds_no.cpp -o c2_prog
c_holds_no.cpp: In function 'int main()':
c_holds_no.cpp:4:10: error: 'variant' is not a member of 'std'
4 | std::variant<int, std::string> v = 42;
| ^~~~~~~
c_holds_no.cpp:2:1: note: 'std::variant' is defined in header '<variant>'; did you forget to '#include <variant>'?
1 | #include <iostream>
+++ |+#include <variant>
2 | #include <string>
c_holds_no.cpp:4:18: error: expected primary-expression before 'int'
4 | std::variant<int, std::string> v = 42;
| ^~~
c_holds_no.cpp:5:23: error: 'holds_alternative' is not a member of 'std'
5 | std::cout << std::holds_alternative<int>(v) << "\n";
| ^~~~~~~~~~~~~~~~~
c_holds_no.cpp:5:23: note: 'std::holds_alternative' is defined in header '<variant>'; did you forget to '#include <variant>'?
c_holds_no.cpp:5:41: error: expected primary-expression before 'int'
5 | std::cout << std::holds_alternative<int>(v) << "\n";
| ^~~
Two more names from the header fail in the same pattern. std::monostate, the unit type used as a well-behaved empty first alternative so a variant of non-default-constructible types can itself be default-constructible, gets its own error after the variant error:
#include <iostream>
int main() {
std::variant<std::monostate, int> v;
std::cout << v.index() << "\n";
return 0;
}
$ g++ -std=c++20 h_mono_no.cpp -o h_prog
h_mono_no.cpp: In function 'int main()':
h_mono_no.cpp:3:10: error: 'variant' is not a member of 'std'
3 | std::variant<std::monostate, int> v;
| ^~~~~~~
h_mono_no.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() {
h_mono_no.cpp:3:23: error: 'monostate' is not a member of 'std'
3 | std::variant<std::monostate, int> v;
| ^~~~~~~~~
h_mono_no.cpp:3:23: note: 'std::monostate' is defined in header '<variant>'; did you forget to '#include <variant>'?
h_mono_no.cpp:3:34: error: expected primary-expression before 'int'
3 | std::variant<std::monostate, int> v;
| ^~~
h_mono_no.cpp:4:18: error: 'v' was not declared in this scope
4 | std::cout << v.index() << "\n";
| ^
And std::get_if, which returns a pointer to the held value or null on error instead of throwing, gets an error of its own — in this output GCC prints no note under the get_if error, unlike the others:
#include <iostream>
#include <string>
int main() {
std::variant<int, std::string> v = 42;
auto* p = std::get_if<int>(&v);
std::cout << (p ? *p : 0) << "\n";
return 0;
}
$ g++ -std=c++20 h_getif_no.cpp -o h2_prog
h_getif_no.cpp: In function 'int main()':
h_getif_no.cpp:4:10: error: 'variant' is not a member of 'std'
4 | std::variant<int, std::string> v = 42;
| ^~~~~~~
h_getif_no.cpp:2:1: note: 'std::variant' is defined in header '<variant>'; did you forget to '#include <variant>'?
1 | #include <iostream>
+++ |+#include <variant>
2 | #include <string>
h_getif_no.cpp:4:18: error: expected primary-expression before 'int'
4 | std::variant<int, std::string> v = 42;
| ^~~
h_getif_no.cpp:5:20: error: 'get_if' is not a member of 'std'
5 | auto* p = std::get_if<int>(&v);
| ^~~~~~
h_getif_no.cpp:5:27: error: expected primary-expression before 'int'
5 | auto* p = std::get_if<int>(&v);
| ^~~
With the header added, all of those names work in one program — visiting prints the held int, the two alternative checks print 1 and 0, asking get_if for the string that is not held prints null, and a variant whose first alternative is monostate reports index 0:
#include <iostream>
#include <string>
#include <variant>
int main() {
std::variant<int, std::string> v = 42;
std::visit([](auto&& x) { std::cout << x << "\n"; }, v);
std::cout << std::holds_alternative<int>(v) << "\n";
std::cout << std::holds_alternative<std::string>(v) << "\n";
if (auto* p = std::get_if<std::string>(&v)) std::cout << *p << "\n"; else std::cout << "null\n";
std::variant<std::monostate, int> w;
std::cout << w.index() << "\n";
return 0;
}
$ g++ -std=c++20 d_fixed.cpp -o d_prog
$ ./d_prog
42
1
0
null
0
One added include declares the template and every helper in this section.
Asking for the wrong alternative
A separate failure waits after the program compiles. std::get by type returns the stored value only when the variant holds that type; asking for another alternative throws std::bad_variant_access, the exception type for invalid access to a variant's value. Catching it prints its message:
#include <iostream>
#include <string>
#include <variant>
int main() {
std::variant<int, std::string> v = 42;
try {
std::cout << std::get<std::string>(v) << "\n";
} catch (const std::bad_variant_access& e) {
std::cout << e.what() << "\n";
}
return 0;
}
$ g++ -std=c++20 e_bad.cpp -o e_prog
$ ./e_prog
std::get: wrong index for variant
That is a runtime error, not a compile error: the include is right, the request is wrong for the value currently held. Checking with holds_alternative or get_if first, or visiting instead, avoids the throw when the held alternative is not known in advance.
A different wording: the name without its qualifier
Including <variant> is not the only half of the name. This program includes the header but drops the std:: qualifier:
#include <iostream>
#include <string>
#include <variant>
int main() {
variant<int, std::string> v = 42;
std::cout << std::get<int>(v) << "\n";
return 0;
}
$ g++ -std=c++20 g_unqualified.cpp -o g_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:5: error: 'variant' was not declared in this scope; did you mean 'std::variant'?
5 | variant<int, std::string> v = 42;
| ^~~~~~~
| std::variant
In file included from g_unqualified.cpp:3:
/usr/include/c++/13/variant:63:38: note: 'std::variant' declared here
63 | template<typename... _Types> class variant;
| ^~~~~~~
g_unqualified.cpp:5:13: error: expected primary-expression before 'int'
5 | variant<int, std::string> v = 42;
| ^~~
g_unqualified.cpp:6:32: error: 'v' was not declared in this scope
6 | std::cout << std::get<int>(v) << "\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::variant; adding includes does not bring the name out of namespace std.
The exact rule
std::variantis declared by#include <variant>, and nowhere else by rule.<string>,<optional>,<any>,<tuple>,<utility>,<memory>, and<type_traits>do not declare it, on this library not even transitively.'variant' is not a member of 'std'means the<variant>include is missing, and GCC's note names the header directly:'std::variant' is defined in header '<variant>'. Follow the note.std::visit,std::holds_alternative,std::get,std::get_if, andstd::monostateare declared by the same#include <variant>: in the programs shown here, which include only<iostream>and<string>beside the use,visit,holds_alternative, andmonostateeach get their ownis not a member of 'std'error with a note naming<variant>, andget_ifgets its own such error without a note.std::bad_variant_accessis also declared by<variant>: it is the exception thrown whenstd::getis asked for an index or type that does not match the currently held alternative.'variant' was not declared in this scopefor an unqualifiedvariantuse is a different defect: qualify the name asstd::variant— 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'v' was not declared in this scope— are knock-on effects of the missing declaration, not separate defects.
The fixes, ranked
- First, check the include list of the file that names
std::variant. If<variant>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 primary-expression and undeclared-
verrors are consequences, not causes. - Third, do not substitute a neighboring header.
<optional>,<any>,<tuple>,<utility>,<memory>, and<type_traits>were each tried here and each fails — there is no transitive rescue to arrange instead. - Fourth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::variantas well. - Fifth, if the failing name is a helper —
visit,holds_alternative,get_if,monostate— the same added include fixes it: every one of those names shares the header. - Sixth, if the program compiles but
std::getthrows at run time, the include is not the problem: check which alternative is held, withholds_alternative,get_if, orindex, before asking for a value.
Which fix for which cause
#include <variant>missing,std::variantused with<iostream>and<string>present: the message is'variant' is not a member of 'std'— add#include <variant>.- Only
<optional>,<any>,<tuple>,<utility>,<memory>, or<type_traits>included: same message, same note — add#include <variant>. std::visitorstd::holds_alternativeused without<variant>: each name gets its ownis not a member of 'std'error with a note naming<variant>— add#include <variant>.std::monostateorstd::get_ifused without<variant>: each name gets its ownis not a member of 'std'error — add#include <variant>.variantused without thestd::qualifier,<variant>included: the message is'variant' was not declared in this scope— qualify the name asstd::variant.std::getasked for a type the variant does not currently hold: the program throwsstd::bad_variant_accessat run time — check the held alternative before asking.
Checklist
- Check the include list of the file that names the variant, 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::variant' is defined in header '<variant>'— so take the fix from the note instead of guessing. - Do not rely on a neighboring header pulling
std::variantin. None of the seven headers tried here does, and no header except<variant>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 the variant. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
42for the direct read,42,hi, and1for the read-back sequence, andint 42,string hi, andhifor the visit sequence. - When the held alternative is not known, prefer
visit,holds_alternative, orget_ifover an unguardedget—getthrowsstd::bad_variant_accesswhen the request does not match the held value.
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 the variant here. - 'std::mutex' Is Not a Member of 'std' Fix — the same missing-include shape for the synchronization class in
<mutex>, whose note likewise names its header.
Sources
- cppreference,
std::variant— forstd::variantbeing a class template (since C++17) that represents a type-safe union, holding a value of one of its alternative types at any given time or no value in the case of error, holding its first alternative when default-constructed, and for theindexmember returning the zero-based index of the alternative held. - cppreference, Standard library header
<variant>— for<variant>declaringstd::variantas a class template,std::monostateandstd::bad_variant_accessas classes, andstd::visit,std::holds_alternative,std::get, andstd::get_ifas function templates. - cppreference,
std::visit— forstd::visit(since C++17) being defined in<variant>and applying the given visitor, a callable that accepts every possible alternative, to the values held by one or more variants. - cppreference,
std::holds_alternative— forstd::holds_alternative(since C++17) checking whether a variant holds the alternativeT, returningtrueif it currently does andfalseotherwise, with the call ill-formed ifTdoes not appear exactly once among the alternatives. - cppreference,
std::get(variant) — forstd::getbeing defined in<variant>, returning a reference to the stored value when the index or unique type matches the held alternative, and throwingstd::bad_variant_accesson error. - cppreference,
std::monostate— forstd::monostate(since C++17) being defined in<variant>, a unit type intended as a well-behaved empty alternative, making a variant of non-default-constructible types default-constructible when listed as its first alternative. - cppreference,
std::bad_variant_access— forstd::bad_variant_access(since C++17) being a class deriving fromstd::exception, thrown whenstd::getis called with an index or type that does not match the currently active alternative, and for itswhat()returning 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.