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

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

By the end of this guide
  • 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::any is a class, since C++17, defined in the header <any>. It is a type-safe container for single values of any copy-constructible type: an object of class any stores an instance of a type that satisfies the constructor requirements, or is empty, and the stored instance is called the contained object.

Because the class lives in one specific header, 'any' is not a member of 'std' has one common cause:

  • The #include <any> line is missing. For this error GCC does name the header: its note says 'std::any' is defined in header '<any>'; did you forget to '#include <any>'?, 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 an any and reads it back, but includes only <iostream> and <string>:

C++
#include <iostream>
#include <string>
int main() {
    std::any a = 42;
    std::cout << std::any_cast<int>(a) << "\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: 'any' is not a member of 'std'
    4 |     std::any a = 42;
      |          ^~~
a_noinclude.cpp:2:1: note: 'std::any' is defined in header '<any>'; did you forget to '#include <any>'?
    1 | #include <iostream>
  +++ |+#include <any>
    2 | #include <string>
a_noinclude.cpp:5:23: error: 'any_cast' is not a member of 'std'
    5 |     std::cout << std::any_cast<int>(a) << "\n";
      |                       ^~~~~~~~
a_noinclude.cpp:5:23: note: 'std::any_cast' is defined in header '<any>'; did you forget to '#include <any>'?
a_noinclude.cpp:5:32: error: expected primary-expression before 'int'
    5 |     std::cout << std::any_cast<int>(a) << "\n";
      |                                ^~~

The first error is the defect. The any_cast error that follows comes from the same missing header — the access function is declared there too — and the primary-expression error is a knock-on effect of the failed any_cast call. 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, 'any' is not a member of 'std':

  • <string>
  • <optional>
  • <variant>
  • <tuple>
  • <utility>
  • <memory>
  • <type_traits>

That list includes headers for the other vocabulary types — <optional> and <variant> — and headers that many programs already pull in for pairs, tuples, and smart pointers. None of them declares std::any on this libstdc++, not even transitively. There is no accidental rescue to rely on, and no shortcut: a program that uses std::any includes <any>.

The fix

Add the header:

C++
#include <any>
#include <iostream>
#include <string>
int main() {
    std::any a = 42;
    std::cout << std::any_cast<int>(a) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 a_fixed.cpp -o a_fixed_prog
$ ./a_fixed_prog
42

One include, <any>, declares the class. std::any_cast<int> reads the value back by type — the value-returning form returns the contained object when the requested type matches the type of the contained object, and throws on a mismatch.

Reading the value back

The same fixed shape handles a different contained type. Assign a string, read it back with any_cast, and ask whether a value is held with the has_value member, which returns true if the object holds a value and false if it is empty. The reset member destroys the contained object, so the second has_value answer changes:

C++
#include <any>
#include <iostream>
#include <string>
int main() {
    std::any a = 42;
    std::cout << std::any_cast<int>(a) << "\n";
    a = std::string("hi");
    std::cout << std::any_cast<std::string>(a) << "\n";
    std::cout << a.has_value() << "\n";
    a.reset();
    std::cout << a.has_value() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 b_fixed.cpp -o b_prog
$ ./b_prog
42
hi
1
0

The output follows the assignments in order: the int prints as 42, the string prints as hi, has_value reports 1 while the string is held, and after reset it reports 0 because the object is empty.

Asking through a pointer

Reading by value works when the caller already knows which type is held. The pointer form of any_cast covers the other case: given a pointer to an any, it returns a pointer to the contained value when the requested type matches the type of the contained object, and a null pointer otherwise. std::make_any builds an any holding a value of a named type:

C++
#include <any>
#include <iostream>
#include <string>
int main() {
    std::any a = 42;
    if (auto* p = std::any_cast<int>(&a)) std::cout << *p << "\n"; else std::cout << "null\n";
    if (auto* p = std::any_cast<std::string>(&a)) std::cout << *p << "\n"; else std::cout << "null\n";
    a = std::make_any<std::string>("hi");
    std::cout << std::any_cast<std::string>(a) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 f_realistic.cpp -o f_prog
$ ./f_prog
42
null
hi

The first pointer request matches the held int and prints 42; the second asks for a string that is not held and prints null instead of throwing; and after the make_any assignment, the value read back prints as hi.

The neighboring errors: the helpers fail too

The helper functions are declared by the same header, so a missing <any> takes them down with the class itself. The first transcript already shows std::any_cast getting its own error with its own note naming <any>. std::make_any, which constructs an any object containing an object of a named type, behaves the same way — and because the object here is declared with auto, the make_any error surfaces first:

C++
#include <iostream>
#include <string>
int main() {
    auto a = std::make_any<int>(42);
    std::cout << std::any_cast<int>(a) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 c_make_no.cpp -o c2_prog
c_make_no.cpp: In function 'int main()':
c_make_no.cpp:4:19: error: 'make_any' is not a member of 'std'
    4 |     auto a = std::make_any<int>(42);
      |                   ^~~~~~~~
c_make_no.cpp:2:1: note: 'std::make_any' is defined in header '<any>'; did you forget to '#include <any>'?
    1 | #include <iostream>
  +++ |+#include <any>
    2 | #include <string>
c_make_no.cpp:4:28: error: expected primary-expression before 'int'
    4 |     auto a = std::make_any<int>(42);
      |                            ^~~
c_make_no.cpp:5:23: error: 'any_cast' is not a member of 'std'
    5 |     std::cout << std::any_cast<int>(a) << "\n";
      |                       ^~~~~~~~
c_make_no.cpp:5:23: note: 'std::any_cast' is defined in header '<any>'; did you forget to '#include <any>'?
c_make_no.cpp:5:32: error: expected primary-expression before 'int'
    5 |     std::cout << std::any_cast<int>(a) << "\n";
      |                                ^~~

With the header added, all of those names work in one program — the value built by make_any reads back as 42, the pointer request for the held type prints 42, the pointer request for a string that is not held prints null, and has_value reports 1:

C++
#include <any>
#include <iostream>
#include <string>
int main() {
    std::any a = std::make_any<int>(42);
    std::cout << std::any_cast<int>(a) << "\n";
    if (auto* p = std::any_cast<int>(&a)) std::cout << *p << "\n"; else std::cout << "null\n";
    if (auto* p = std::any_cast<std::string>(&a)) std::cout << *p << "\n"; else std::cout << "null\n";
    std::cout << a.has_value() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 d_fixed.cpp -o d_prog
$ ./d_prog
42
42
null
1

One added include declares the class and every helper in this section.

Asking for the wrong type

A separate failure waits after the program compiles. The value-returning form of std::any_cast returns the contained object only when the requested type matches the type of the contained object; asking for another type throws std::bad_any_cast, the exception type thrown by the value-returning forms of any_cast on a type mismatch. Catching it prints its message:

C++
#include <any>
#include <iostream>
#include <string>
int main() {
    std::any a = 42;
    try {
        std::cout << std::any_cast<std::string>(a) << "\n";
    } catch (const std::bad_any_cast& e) {
        std::cout << e.what() << "\n";
    }
    return 0;
}
Terminal
$ g++ -std=c++20 e_bad.cpp -o e_prog
$ ./e_prog
bad any_cast

That is a runtime error, not a compile error: the include is right, the request is wrong for the value currently held. Asking through the pointer form first, or checking has_value and the held type before asking, avoids the throw when the contained type is not known in advance.

A different wording: the name without its qualifier

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

C++
#include <any>
#include <iostream>
#include <string>
int main() {
    any a = 42;
    std::cout << std::any_cast<int>(a) << "\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: 'any' was not declared in this scope; did you mean 'std::any'?
    5 |     any a = 42;
      |     ^~~
      |     std::any
In file included from g_unqualified.cpp:1:
/usr/include/c++/13/any:80:9: note: 'std::any' declared here
   80 |   class any
      |         ^~~
g_unqualified.cpp:6:37: error: 'a' was not declared in this scope
    6 |     std::cout << std::any_cast<int>(a) << "\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::any; adding includes does not bring the name out of namespace std.

The exact rule

  • std::any is declared by #include <any>, and nowhere else by rule. <string>, <optional>, <variant>, <tuple>, <utility>, <memory>, and <type_traits> do not declare it, on this library not even transitively.
  • 'any' is not a member of 'std' means the <any> include is missing, and GCC's note names the header directly: 'std::any' is defined in header '<any>'. Follow the note.
  • std::any_cast and std::make_any are declared by the same #include <any>: in the programs shown here, which include only <iostream> and <string> beside the use, each name gets its own is not a member of 'std' error with a note naming <any>.
  • std::bad_any_cast is also declared by <any>: it is the exception thrown when a value-returning any_cast is asked for a type that does not match the contained object.
  • 'any' was not declared in this scope for an unqualified any use is a different defect: qualify the name as std::any — 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 'a' 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::any. If <any> 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-a errors are consequences, not causes.
  • Third, do not substitute a neighboring header. <optional>, <variant>, <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 scope instead, fix the name's qualifier to std::any as well.
  • Fifth, if the failing name is a helper — any_cast, make_any — the same added include fixes it: every one of those names shares the header.
  • Sixth, if the program compiles but any_cast throws at run time, the include is not the problem: check which type is held, with the pointer form of any_cast, before asking for a value.

Which fix for which cause

  • #include <any> missing, std::any used with <iostream> and <string> present: the message is 'any' is not a member of 'std' — add #include <any>.
  • Only <optional>, <variant>, <tuple>, <utility>, <memory>, or <type_traits> included: same message, same note — add #include <any>.
  • std::any_cast or std::make_any used without <any>: each name gets its own is not a member of 'std' error with a note naming <any> — add #include <any>.
  • any used without the std:: qualifier, <any> included: the message is 'any' was not declared in this scope — qualify the name as std::any.
  • A value-returning std::any_cast asked for a type the any does not currently hold: the program throws std::bad_any_cast at run time — check the held type before asking.

Checklist

  • Check the include list of the file that names the any, 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::any' is defined in header '<any>' — so take the fix from the note instead of guessing.
  • Do not rely on a neighboring header pulling std::any in. None of the seven headers tried here does, and no header except <any> is required to.
  • One header per name the file uses: std::string comes from <string> and output from <iostream>, even in a program whose subject is the any.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 42 for the direct read, 42, hi, 1, and 0 for the read-back sequence, and 42, null, and hi for the pointer-form sequence.
  • When the contained type is not known, prefer the pointer form of any_cast over an unguarded value request — the value form throws std::bad_any_cast when the request does not match the held value.

Sources

  • cppreference, std::any — for std::any being a class (since C++17) defined in <any> that describes a type-safe container for single values of any copy-constructible type, storing an instance of a type that satisfies the constructor requirements or being empty, for the non-member any_cast functions providing type-safe access to the contained object, and for the has_value and reset members.
  • cppreference, Standard library header <any> — for <any> declaring std::any and std::bad_any_cast as classes, and std::make_any and std::any_cast as function templates.
  • cppreference, std::any_cast — for std::any_cast (since C++17) being defined in <any> and performing type-safe access to the contained object, with the pointer forms returning a pointer to the contained value when the requested type matches the type of the contents and a null pointer otherwise.
  • cppreference, std::make_any — for std::make_any (since C++17) being defined in <any> and constructing an any object containing an object of the named type, passing the provided arguments to that type's constructor.
  • cppreference, std::bad_any_cast — for std::bad_any_cast (since C++17) being a class deriving from std::bad_cast, thrown by the value-returning forms of any_cast on a type mismatch, and for its what() returning the explanatory string.

Keep Exploring