C++

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

std::atomic lives in one header, <atomic> - and in a file that never includes it, GCC says so in a note under this error. The confusing part is that the same code can compile in the next file over, because a few threading headers declare atomic on the side. This guide pins down when the error appears, which probed headers do and do not cause it, and the one include that fixes it by specification.

Written November 30, 2026 · Tested with GCC 13.3.0 · 13 min read

By the end of this guide
  • The error: the include is missing
  • Neighboring headers: some fail, one half-works, some only appear to work
  • The fix
  • The atomic counter: four threads, no lock

Written November 30, 2026

Tested with GCC 13.3.0

What this guide is

std::atomic is a class template defined in the header <atomic>. Each instantiation defines an atomic type: if one thread writes to an atomic object while another thread reads from it, the behavior is well-defined, and the integral specializations add operations such as fetch_add and fetch_sub on top of load and store. The template is neither copyable nor movable.

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

  • The #include <atomic> line is missing. For this error GCC does name the header: its note says 'std::atomic' is defined in header '<atomic>'; did you forget to '#include <atomic>'?, and following that note is the whole fix.

Some headers that sit near atomic code do not rescue the program, one only appears to, and a few rescue it only by accident. Each one is separated below.

The error: the include is missing

This program declares a std::atomic<int>, adds to it, and prints it, but includes only <iostream>:

C++
#include <iostream>
int main() {
    std::atomic<int> counter{0};
    counter.fetch_add(1);
    std::cout << counter.load() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 a_noinclude.cpp -o a_prog
a_noinclude.cpp: In function 'int main()':
a_noinclude.cpp:3:10: error: 'atomic' is not a member of 'std'
    3 |     std::atomic<int> counter{0};
      |          ^~~~~~
a_noinclude.cpp:2:1: note: 'std::atomic' is defined in header '<atomic>'; did you forget to '#include <atomic>'?
    1 | #include <iostream>
  +++ |+#include <atomic>
    2 | int main() {
a_noinclude.cpp:3:17: error: expected primary-expression before 'int'
    3 |     std::atomic<int> counter{0};
      |                 ^~~
a_noinclude.cpp:4:5: error: 'counter' was not declared in this scope
    4 |     counter.fetch_add(1);
      |     ^~~~~~~

The first error is the defect. The expected primary-expression and 'counter' was not declared in this scope errors that follow are knock-on effects: the declaration of counter failed, so the template argument and every later use of counter fail too. Fix the include and all of them go.

Neighboring headers: some fail, one half-works, some only appear to work

Swapping <iostream> for another nearby header changes nothing for most of them. In a sweep where each header below was compiled as the only header in a program that declares a std::atomic<int> and returns its loaded value, each of these fails with the same first error, 'atomic' is not a member of 'std':

  • <mutex>
  • <vector>
  • <string>
  • <algorithm>
  • <utility>
  • <chrono>
  • <shared_mutex>

Note <mutex> in that list: the mutex header does not declare std::atomic, even though mutex and atomic code often sit in the same file.

One header in the sweep behaves differently, and the difference is a trap of its own. <memory> forward-declares the template, so the 'atomic' is not a member of 'std' error disappears - but the type stays incomplete and the program still does not compile:

C++
#include <iostream>
#include <memory>
int main() {
    std::atomic<int> counter{0};
    counter.fetch_add(1);
    std::cout << counter.load() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 e_memoryonly.cpp -o e2_prog
e_memoryonly.cpp: In function 'int main()':
e_memoryonly.cpp:4:22: error: variable 'std::atomic<int> counter' has initializer but incomplete type
    4 |     std::atomic<int> counter{0};
      |                      ^~~~~~~

A disappearing title error is not a fix: with only <memory>, the name exists but no object of the type can be created.

Three other headers in the sweep behave differently again. This program includes only <thread> besides <iostream>:

C++
#include <iostream>
#include <thread>
int main() {
    std::atomic<int> counter{0};
    counter.fetch_add(1);
    std::cout << counter.load() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 e_threadonly.cpp -o e_prog
$ ./e_prog
1

It compiles and prints 1. The same happens in the sweep with <condition_variable> and with <future> as the only header: on this libstdc++, each of those headers pulls <atomic> in transitively, so the name is fully declared even though the program never includes <atomic> itself. That is a property of this library version, not a rule of the language - no header except <atomic> is required to declare std::atomic, and another library, or another version of this one, may stop pulling it in. A program that uses std::atomic includes <atomic>, whether or not it happens to compile without it.

The fix

Add the header:

C++
#include <atomic>
#include <iostream>
int main() {
    std::atomic<int> counter{0};
    int before = counter.fetch_add(1);
    counter.store(10);
    std::cout << before << "\n";
    std::cout << counter.load() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 h_fixed.cpp -o h_prog
$ ./h_prog
0
10

One include, <atomic>, declares the template. fetch_add returns the value held before the addition, so the program prints 0 for the value before the add and 10 for the value stored afterward.

The atomic counter: four threads, no lock

The point of the type is sharing it between threads without a lock. Four threads each add 1 to the same counter 250 times, and fetch_add makes every addition atomic, so no increment is lost:

C++
#include <atomic>
#include <iostream>
#include <thread>
#include <vector>
int main() {
    std::atomic<int> counter{0};
    std::vector<std::thread> workers;
    for (int t = 0; t < 4; ++t) {
        workers.emplace_back([&]() {
            for (int i = 0; i < 250; ++i) counter.fetch_add(1);
        });
    }
    for (auto& w : workers) w.join();
    std::cout << counter.load() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 i_threads.cpp -o i_prog
$ ./i_prog
1000

The program prints 1000, every run: 4 threads times 250 atomic increments, with no mutex anywhere in it. The <thread> and <vector> includes are there for the names they themselves declare - std::thread and std::vector - not as a route to <atomic>.

The neighboring names: the whole header family fails too

The other names declared by <atomic> fail with the same shape of error when it is missing. std::atomic<bool> - the bool specialization, reached through the primary template - gets the same error and the same note as std::atomic<int>:

C++
#include <iostream>
int main() {
    std::atomic<bool> ready{false};
    ready.store(true);
    std::cout << ready.load() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 b_bool.cpp -o b_prog
b_bool.cpp: In function 'int main()':
b_bool.cpp:3:10: error: 'atomic' is not a member of 'std'
    3 |     std::atomic<bool> ready{false};
      |          ^~~~~~
b_bool.cpp:2:1: note: 'std::atomic' is defined in header '<atomic>'; did you forget to '#include <atomic>'?
    1 | #include <iostream>
  +++ |+#include <atomic>
    2 | int main() {
b_bool.cpp:3:17: error: expected primary-expression before 'bool'
    3 |     std::atomic<bool> ready{false};
      |                 ^~~~
b_bool.cpp:4:5: error: 'ready' was not declared in this scope
    4 |     ready.store(true);
      |     ^~~~~

std::atomic_flag - the lock-free boolean atomic type, which unlike std::atomic<bool> provides no load or store operations - fails the same way, except that GCC prints no header note under this error:

C++
#include <iostream>
int main() {
    std::atomic_flag flag = ATOMIC_FLAG_INIT;
    bool was = flag.test_and_set();
    std::cout << was << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 c_flag.cpp -o c_prog
c_flag.cpp: In function 'int main()':
c_flag.cpp:3:10: error: 'atomic_flag' is not a member of 'std'
    3 |     std::atomic_flag flag = ATOMIC_FLAG_INIT;
      |          ^~~~~~~~~~~
c_flag.cpp:4:16: error: 'flag' was not declared in this scope
    4 |     bool was = flag.test_and_set();
      |                ^~~~

The free functions fail too. std::atomic_store and std::atomic_load are declared by the same header, so after the atomic error itself, each function gets its own is not a member of 'std' error:

C++
#include <iostream>
int main() {
    std::atomic<int> counter{0};
    std::atomic_store(&counter, 3);
    std::cout << std::atomic_load(&counter) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 d_free.cpp -o d_prog
d_free.cpp: In function 'int main()':
d_free.cpp:3:10: error: 'atomic' is not a member of 'std'
    3 |     std::atomic<int> counter{0};
      |          ^~~~~~
d_free.cpp:2:1: note: 'std::atomic' is defined in header '<atomic>'; did you forget to '#include <atomic>'?
    1 | #include <iostream>
  +++ |+#include <atomic>
    2 | int main() {
d_free.cpp:3:17: error: expected primary-expression before 'int'
    3 |     std::atomic<int> counter{0};
      |                 ^~~
d_free.cpp:4:10: error: 'atomic_store' is not a member of 'std'
    4 |     std::atomic_store(&counter, 3);
      |          ^~~~~~~~~~~~
d_free.cpp:4:24: error: 'counter' was not declared in this scope
    4 |     std::atomic_store(&counter, 3);
      |                        ^~~~~~~
d_free.cpp:5:23: error: 'atomic_load' is not a member of 'std'
    5 |     std::cout << std::atomic_load(&counter) << "\n";
      |                       ^~~~~~~~~~~

std::memory_order - the enumeration that specifies how memory accesses are ordered around an atomic operation - is declared by <atomic> as well, and fails the same way, again with no header note from GCC:

C++
#include <iostream>
int main() {
    std::memory_order order = std::memory_order::seq_cst;
    std::cout << static_cast<int>(order) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 e_order.cpp -o e_prog
e_order.cpp: In function 'int main()':
e_order.cpp:3:10: error: 'memory_order' is not a member of 'std'
    3 |     std::memory_order order = std::memory_order::seq_cst;
      |          ^~~~~~~~~~~~
e_order.cpp:4:35: error: 'order' was not declared in this scope
    4 |     std::cout << static_cast<int>(order) << "\n";
      |                                   ^~~~~

And std::atomic_ref - the C++20 class template that applies atomic operations to a non-atomic object it references - fails with its own error and its own note naming <atomic>:

C++
#include <iostream>
int main() {
    int value = 0;
    std::atomic_ref<int> ref(value);
    ref.fetch_add(1);
    std::cout << value << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 f_ref.cpp -o f_prog
f_ref.cpp: In function 'int main()':
f_ref.cpp:4:10: error: 'atomic_ref' is not a member of 'std'
    4 |     std::atomic_ref<int> ref(value);
      |          ^~~~~~~~~~
f_ref.cpp:2:1: note: 'std::atomic_ref' is defined in header '<atomic>'; did you forget to '#include <atomic>'?
    1 | #include <iostream>
  +++ |+#include <atomic>
    2 | int main() {
f_ref.cpp:4:21: error: expected primary-expression before 'int'
    4 |     std::atomic_ref<int> ref(value);
      |                     ^~~
f_ref.cpp:5:5: error: 'ref' was not declared in this scope; did you mean 'std::ref'?
    5 |     ref.fetch_add(1);
      |     ^~~
      |     std::ref
In file included from /usr/include/c++/13/string:52,
                 from /usr/include/c++/13/bits/locale_classes.h:40,
                 from /usr/include/c++/13/bits/ios_base.h:41,
                 from /usr/include/c++/13/ios:44,
                 from /usr/include/c++/13/ostream:40,
                 from /usr/include/c++/13/iostream:41,
                 from f_ref.cpp:1:
/usr/include/c++/13/bits/refwrap.h:394:5: note: 'std::ref' declared here
  394 |     ref(reference_wrapper<_Tp> __t) noexcept
      |     ^~~

One added include declares every one of these names: the template, the flag, the free functions, the memory-order enumeration, and the reference wrapper.

The neighboring names, fixed

With <atomic> added, the flag, the free functions, the memory-order argument, and the reference wrapper all work in short programs. The flag's test_and_set returns the previous value - 0 the first time, 1 the second - and after clear, test reports 0:

C++
#include <atomic>
#include <iostream>
int main() {
    std::atomic_flag flag = ATOMIC_FLAG_INIT;
    bool first = flag.test_and_set();
    bool second = flag.test_and_set();
    flag.clear();
    bool third = flag.test();
    std::cout << first << "\n";
    std::cout << second << "\n";
    std::cout << third << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 j_flag_fixed.cpp -o j_prog
$ ./j_prog
0
1
0

The free functions mirror the member functions: std::atomic_store sets the counter to 3, std::atomic_fetch_add returns the value held before the addition, and std::atomic_load reads the result:

C++
#include <atomic>
#include <iostream>
int main() {
    std::atomic<int> counter{0};
    std::atomic_store(&counter, 3);
    int before = std::atomic_fetch_add(&counter, 2);
    std::cout << before << "\n";
    std::cout << std::atomic_load(&counter) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 m_free_fixed.cpp -o m_prog
$ ./m_prog
3
5

A std::memory_order argument rides on the member functions - here a release store and an acquire load of the same counter:

C++
#include <atomic>
#include <iostream>
int main() {
    std::atomic<int> counter{0};
    counter.store(7, std::memory_order_release);
    int value = counter.load(std::memory_order_acquire);
    std::cout << value << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 l_order_fixed.cpp -o l_prog
$ ./l_prog
7

And std::atomic_ref adds atomically to a plain int through the reference, so the underlying variable itself changes:

C++
#include <atomic>
#include <iostream>
int main() {
    int value = 0;
    std::atomic_ref<int> ref(value);
    ref.fetch_add(5);
    std::cout << value << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_ref_fixed.cpp -o k_prog
$ ./k_prog
5

A different wording: the name without its qualifier

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

C++
#include <atomic>
#include <iostream>
int main() {
    atomic<int> counter{0};
    counter.fetch_add(1);
    std::cout << counter.load() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 g_unqualified.cpp -o g_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:5: error: 'atomic' was not declared in this scope; did you mean 'std::atomic'?
    4 |     atomic<int> counter{0};
      |     ^~~~~~
      |     std::atomic
In file included from /usr/include/c++/13/atomic:41,
                 from g_unqualified.cpp:1:
/usr/include/c++/13/bits/atomic_base.h:174:12: note: 'std::atomic' declared here
  174 |     struct atomic;
      |            ^~~~~~
g_unqualified.cpp:4:12: error: expected primary-expression before 'int'
    4 |     atomic<int> counter{0};
      |            ^~~
g_unqualified.cpp:5:5: error: 'counter' was not declared in this scope
    5 |     counter.fetch_add(1);
      |     ^~~~~~~

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::atomic; adding includes does not bring the name out of namespace std.

The exact rule

  • std::atomic is declared by #include <atomic>, and nowhere else by rule. In the sweep probed here, <iostream>, <mutex>, <vector>, <string>, <algorithm>, <utility>, <chrono>, and <shared_mutex> do not declare it.
  • 'atomic' is not a member of 'std' means the <atomic> include is missing, and GCC's note names the header directly: 'std::atomic' is defined in header '<atomic>'. Follow the note.
  • <memory> forward-declares std::atomic on this libstdc++, so the title error disappears but the type is incomplete and no object can be created. A forward declaration is not a declaration you can use.
  • In the sweep probed here, <thread>, <condition_variable>, and <future> happen to declare std::atomic fully on this libstdc++ because they pull <atomic> in transitively. That is not a guarantee - a file that uses std::atomic includes <atomic> itself.
  • std::atomic_flag, std::atomic_ref, std::memory_order, and the free functions std::atomic_store, std::atomic_load, and std::atomic_fetch_add are declared by the same #include <atomic>: in the programs shown here, which include only <iostream> beside the use, each gets its own is not a member of 'std' error when that include is missing - with a header note for atomic and atomic_ref, and without one for atomic_flag and memory_order.
  • 'atomic' was not declared in this scope for an unqualified atomic use is a different defect: qualify the name as std::atomic - the include alone does not bring the name out of namespace std.
  • The expected primary-expression and was not declared in this scope errors that follow a failed declaration 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::atomic. If <atomic> 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 undeclared-counter and expected-expression errors are consequences, not causes.
  • Third, if the program compiles only because <thread>, <condition_variable>, or <future> is present, add <atomic> anyway - the transitive include is this library's accident, not the program's arrangement.
  • Fourth, if the title error is gone but the error now says the type is incomplete, a header such as <memory> has only forward-declared the template - add <atomic> for the full declaration.
  • Fifth, if the error says was not declared in this scope instead, fix the name's qualifier to std::atomic as well.
  • Sixth, if the failing name is a neighboring one - atomic_flag, atomic_ref, memory_order, or a free function such as atomic_store - the same added include fixes it: every one of those names shares the header.

Which fix for which cause

  • #include <atomic> missing, std::atomic used with <iostream> present: the message is 'atomic' is not a member of 'std' - add #include <atomic>.
  • Only a header from the sweep's failing set included - <mutex>, <vector>, <string>, <algorithm>, <utility>, <chrono>, or <shared_mutex>: same message, same note - add #include <atomic>.
  • Only <memory> included: the message is has initializer but incomplete type - add #include <atomic> for the full declaration.
  • Only <thread>, <condition_variable>, or <future> included: the program compiles on this library, by transitive include - add #include <atomic> anyway so it compiles by rule.
  • std::atomic_flag or std::memory_order used without <atomic>: the message names that name, with no header note from GCC - add #include <atomic>.
  • std::atomic_ref used without <atomic>: the message is 'atomic_ref' is not a member of 'std' with a note naming <atomic> - add #include <atomic>.
  • std::atomic_store, std::atomic_load, or another free atomic function used without <atomic>: each function gets its own is not a member of 'std' error - add #include <atomic>.
  • atomic used without the std:: qualifier, <atomic> included: the message is 'atomic' was not declared in this scope - qualify the name as std::atomic.

Checklist

  • Check the include list of the file that names the atomic, 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::atomic' is defined in header '<atomic>' - so take the fix from the note instead of guessing. Two neighboring names, atomic_flag and memory_order, get no note in the programs here; the fix is the same header.
  • Do not rely on a neighboring header pulling std::atomic in. In the sweep here, <thread>, <condition_variable>, and <future> do on this libstdc++, <memory> only forward-declares it, and no header is required to do either.
  • One header per name the file uses: std::thread comes from <thread> and std::mutex from <mutex>, even in a program whose subject is the atomic - and <mutex> does not declare std::atomic in return.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 0 and 10 for the fetch-add and store sequence, 1000 for the four-thread counter, 0, 1, and 0 across the flag sequence, 3 and 5 for the free functions, 7 for the memory-order load, and 5 for the atomic reference.

Sources

  • cppreference, std::atomic - for std::atomic being a class template defined in <atomic> (since C++11), each instantiation defining an atomic type whose concurrent read/write behavior is well-defined, the integral specializations providing fetch_add and fetch_sub, and the template being neither copyable nor movable.
  • cppreference, Standard library header <atomic> - for <atomic> declaring std::atomic, std::atomic_ref (since C++20), and std::atomic_flag, the std::memory_order enumeration, and the free functions std::atomic_store, std::atomic_load, and std::atomic_fetch_add.
  • cppreference, std::atomic_flag - for std::atomic_flag being an atomic boolean type (since C++11) that is guaranteed to be lock-free and, unlike std::atomic<bool>, does not provide load or store operations, with test_and_set, clear, and (since C++20) test as its operations.
  • cppreference, std::atomic_ref - for std::atomic_ref being a class template (since C++20) that applies atomic operations to the non-atomic object it references, with the referenced object treated as an atomic object for the reference's lifetime.
  • cppreference, std::memory_order - for std::memory_order being defined in <atomic> (since C++11) and specifying how memory accesses, including non-atomic ones, are ordered around an atomic operation, with sequentially consistent ordering as the default.

Keep Exploring