C++

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

std::swap lives in one header, <utility> - but on this library version most ordinary files never see this error, because so many common headers pull the name in on the side. It surfaces in lean files, and behind a handful of headers it changes shape entirely: the name is declared but not defined, and the failure moves to the link. This guide pins down all three outcomes, which probed headers cause each one, and the one include that fixes the error by specification.

Written December 1, 2026 · Tested with GCC 13.3.0 · 17 min read

By the end of this guide
  • The error: the include is missing, in a lean file
  • The second failure: declared, but not defined
  • The textbook version does not fail - the common headers rescue it
  • Neighboring headers: which ones fail, which ones link-fail, which ones rescue it

Written December 1, 2026

Tested with GCC 13.3.0

What this guide is

std::swap is a function template defined in the header <utility> since C++11 - before C++11 it was defined in <algorithm>. It exchanges the given values: the first overload swaps the values of two objects of the same type, and the second overload swaps two arrays element by element, in effect calling std::swap_ranges on them. Since C++20 both overloads are constexpr.

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

  • The #include <utility> line is missing, and no other header in the file pulls the name in transitively. For this error GCC prints no note naming <utility> and no suggestion, so the fix has to come from knowing the name's home, not from reading a note. That silence is specific to swap in the probed set: std::exchange and std::move, which live in the same header, do get a note naming <utility> when they fail the same way, as the neighboring-names section shows.

What makes this error unusual is how rarely it survives contact with an ordinary file. Most common headers rescue it on this library version - the textbook version below compiles - so the plain error is a lean-file error, and five headers produce a third outcome, a link failure, that neither of the other shapes predicts. Each case is separated below.

The error: the include is missing, in a lean file

This program swaps two integers with std::swap, but includes only <limits>:

C++
#include <limits>
int main() {
    int a = 1, b = 2;
    std::swap(a, b);
    return a == 2 && b == 1 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:10: error: 'swap' is not a member of 'std'
    4 |     std::swap(a, b);
      |          ^~~~

That is the whole output: one error, no note, no suggestion. The same program with no include at all fails the same way:

C++
int main() {
    int a = 1, b = 2;
    std::swap(a, b);
    return a == 2 && b == 1 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:10: error: 'swap' is not a member of 'std'
    3 |     std::swap(a, b);
      |          ^~~~

The second failure: declared, but not defined

A file whose only include is <type_traits> never sees the title error at all. The name is declared - compiling the file to an object succeeds - but the definition is not there, so the link fails with an undefined reference to std::swap<int>:

C++
#include <type_traits>
int main() {
    int a = 1, b = 2;
    std::swap(a, b);
    return a == 2 && b == 1 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 -c a_type_traits.cpp -o a_type_traits.o
$ g++ a_type_traits.o -o a_prog
/usr/bin/ld: a_type_traits.o: in function `main':
a_type_traits.cpp:(.text+0x38): undefined reference to `std::enable_if<std::__and_<std::__not_<std::__is_tuple_like<int> >, std::is_move_constructible<int>, std::is_move_assignable<int> >::value, void>::type std::swap<int>(int&, int&)'
collect2: error: ld returned 1 exit status

Read the two steps separately. The first command produces the object file with no diagnostic; the second fails in the linker, and its message names the missing symbol rather than a missing member. This is not a different defect and it does not need a different fix: the declaration the file picked up is not the header that defines the template, and adding <utility> - the fix below - supplies the definition. In the sweep probed here, <compare>, <concepts>, <ratio>, and <bit> produce this same link outcome as sole includes; no other probed header does.

The textbook version does not fail - the common headers rescue it

The version most often written first - swap two elements of a vector and print the result with std::cout - compiles and runs without <utility>:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 3};
    std::swap(v[0], v[2]);
    for (std::size_t i = 0; i < v.size(); ++i) std::cout << (i ? " " : "") << v[i];
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
$ ./t_textbook_prog
3 2 1

What happened: on this library version, <vector> and <iostream> each pull the name in transitively, so the call succeeds in a textbook file even though neither header is required to declare std::swap. That rescue is a property of this library version, not a rule of the language - no header except <utility> is required to declare std::swap, and another library could leave it out of all of them. A program that uses std::swap includes <utility>, whether or not it happens to compile without it.

In a sweep where each header below was compiled as the only header in a program that swaps two integers and returns whether they exchanged places, the outcome splits three ways. These headers fail with the plain is not a member of 'std' error, with no note and no suggestion:

  • <limits>
  • <cstring>
  • <cstdint>
  • <cstddef>
  • <initializer_list>
  • <version>
  • <climits>
  • <cctype>
  • <new>
  • <typeinfo>

A file with no include at all fails the same way. These headers produce the second failure instead - the file compiles to an object, and the link fails with an undefined reference to std::swap<int>:

  • <type_traits>
  • <compare>
  • <concepts>
  • <ratio>
  • <bit>

And these headers rescue the program - it compiles and runs with no <utility> anywhere in the file:

  • <algorithm>
  • <vector>
  • <string>
  • <iostream>
  • <memory>
  • <optional>
  • <array>
  • <numeric>
  • <functional>
  • <tuple>
  • <map>
  • <set>
  • <thread>
  • <mutex>
  • <chrono>
  • <stdexcept>
  • <exception>
  • <iterator>
  • <system_error>

The rescue list is the long one, and it includes <algorithm>, the header std::swap lived in before C++11. Every rescue on that list is this library version's behavior, not a guarantee.

The fix

Add the header:

C++
#include <iostream>
#include <utility>
int main() {
    int a = 1, b = 2;
    std::swap(a, b);
    std::cout << a << " " << b << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
2 1

One include, <utility>, declares and defines the template. std::swap(a, b) exchanges the two values, so the program prints the integers in exchanged order: 2 1.

The neighboring names: two headers, two note behaviors

The names that sit next to std::swap split by home header and by whether GCC helps. std::exchange - the function template in <utility> since C++14 that replaces its first argument with a new value and returns the argument's previous value - lives in the same header as swap, fails with the same shape of error in the same lean context, and does get a note naming <utility>:

C++
#include <limits>
int main() {
    int x = 1;
    int old = std::exchange(x, 5);
    return old == 1 && x == 5 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 b_exchange.cpp -o b_exchange_prog
b_exchange.cpp: In function 'int main()':
b_exchange.cpp:4:20: error: 'exchange' is not a member of 'std'
    4 |     int old = std::exchange(x, 5);
      |                    ^~~~~~~~
b_exchange.cpp:2:1: note: 'std::exchange' is defined in header '<utility>'; did you forget to '#include <utility>'?
    1 | #include <limits>
  +++ |+#include <utility>
    2 | int main() {

std::move, the cast to an rvalue reference in the same header, behaves the same way - its own error, and its own note naming <utility>:

C++
#include <limits>
int main() {
    int a = 1;
    int b = std::move(a);
    return b == 1 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 e_move.cpp -o e_move_prog
e_move.cpp: In function 'int main()':
e_move.cpp:4:18: error: 'move' is not a member of 'std'
    4 |     int b = std::move(a);
      |                  ^~~~
e_move.cpp:2:1: note: 'std::move' is defined in header '<utility>'; did you forget to '#include <utility>'?
    1 | #include <limits>
  +++ |+#include <utility>
    2 | int main() {

The two algorithm names live in the other header. std::swap_ranges - the function template defined in <algorithm> that exchanges the elements of the range [first1, last1) with the elements of the range beginning at first2 - gets its own error, again with no header note:

C++
#include <limits>
int main() {
    int a[] = {1, 2};
    int b[] = {3, 4};
    std::swap_ranges(a, a + 2, b);
    return a[0] == 3 && b[0] == 1 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 c_swap_ranges.cpp -o c_swap_ranges_prog
c_swap_ranges.cpp: In function 'int main()':
c_swap_ranges.cpp:5:10: error: 'swap_ranges' is not a member of 'std'
    5 |     std::swap_ranges(a, a + 2, b);
      |          ^~~~~~~~~~~

And std::iter_swap - the function template defined in <algorithm> that swaps the elements pointed to by two iterators - fails the same way:

C++
#include <limits>
int main() {
    int a[] = {1, 2};
    std::iter_swap(a, a + 1);
    return a[0] == 2 && a[1] == 1 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 d_iter_swap.cpp -o d_iter_swap_prog
d_iter_swap.cpp: In function 'int main()':
d_iter_swap.cpp:4:10: error: 'iter_swap' is not a member of 'std'
    4 |     std::iter_swap(a, a + 1);
      |          ^~~~~~~~~

One contrast is worth holding on to: in the programs here, swap, swap_ranges, and iter_swap get no note, while exchange and move get a note naming <utility>. Do not read the absence of a note as evidence about which header a name lives in - in this probed set, the silent names span both headers, and the noted names are the fix's own header-mates.

The neighboring names, fixed

With <utility> added, std::swap works on more than two integers. The strings "alpha" and "beta" print in exchanged order as beta alpha, and swapping two whole vectors exchanges their contents - the first vector prints 9 8 and the second prints 1 2:

C++
#include <iostream>
#include <string>
#include <utility>
#include <vector>
int main() {
    std::string s1 = "alpha", s2 = "beta";
    std::swap(s1, s2);
    std::cout << s1 << " " << s2 << "\n";
    std::vector<int> x = {1, 2}, y = {9, 8};
    std::swap(x, y);
    for (std::size_t i = 0; i < x.size(); ++i) std::cout << (i ? " " : "") << x[i];
    std::cout << "\n";
    for (std::size_t i = 0; i < y.size(); ++i) std::cout << (i ? " " : "") << y[i];
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 i_string_vector.cpp -o i_string_vector_prog
$ ./i_string_vector_prog
beta alpha
9 8
1 2

The array overload swaps two arrays element by element, so the array that held 1 2 3 prints 7 8 9 and the array that held 7 8 9 prints 1 2 3:

C++
#include <iostream>
#include <utility>
int main() {
    int a[] = {1, 2, 3};
    int b[] = {7, 8, 9};
    std::swap(a, b);
    for (int i = 0; i < 3; ++i) std::cout << (i ? " " : "") << a[i];
    std::cout << "\n";
    for (int i = 0; i < 3; ++i) std::cout << (i ? " " : "") << b[i];
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 j_array.cpp -o j_array_prog
$ ./j_array_prog
7 8 9
1 2 3

And one added <algorithm> beside the <utility> fixes the neighboring names together: std::exchange prints the previous value and the new one, 1 5; std::swap_ranges exchanges the two arrays, printing 3 4 and 1 2; and std::iter_swap exchanges the two pointed-to elements, printing 2 1:

C++
#include <algorithm>
#include <iostream>
#include <utility>
int main() {
    int x = 1;
    int old = std::exchange(x, 5);
    std::cout << old << " " << x << "\n";
    int a[] = {1, 2};
    int b[] = {3, 4};
    std::swap_ranges(a, a + 2, b);
    std::cout << a[0] << " " << a[1] << "\n";
    std::cout << b[0] << " " << b[1] << "\n";
    int c[] = {1, 2};
    std::iter_swap(c, c + 1);
    std::cout << c[0] << " " << c[1] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_neighbors_fixed.cpp -o k_neighbors_fixed_prog
$ ./k_neighbors_fixed_prog
1 5
3 4
1 2
2 1

Since C++20 the same <utility> header also declares std::ranges::swap, the customization point object form of the same operation. With <utility> included it swaps the two integers and the program prints 2 1; in the lean context - only <limits> included - it fails with its own wording, 'std::ranges' has not been declared, because the namespace itself is not declared there:

C++
#include <iostream>
#include <utility>
int main() {
    int a = 1, b = 2;
    std::ranges::swap(a, b);
    std::cout << a << " " << b << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
2 1
C++
#include <limits>
int main() {
    int a = 1, b = 2;
    std::ranges::swap(a, b);
    return a == 2 && b == 1 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 r2_ranges_lean.cpp -o r2_ranges_lean_prog
r2_ranges_lean.cpp: In function 'int main()':
r2_ranges_lean.cpp:4:10: error: 'std::ranges' has not been declared
    4 |     std::ranges::swap(a, b);
      |          ^~~~~~

A different wording: the name without its qualifier

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

C++
#include <iostream>
#include <utility>
int main() {
    int a = 1, b = 2;
    swap(a, b);
    std::cout << a << " " << b << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:5: error: 'swap' was not declared in this scope
    5 |     swap(a, b);
      |     ^~~~
g_unqualified.cpp:5:5: note: suggested alternatives:
In file included from /usr/include/c++/13/bits/uses_allocator_args.h:38,
                 from /usr/include/c++/13/bits/memory_resource.h:41,
                 from /usr/include/c++/13/string:58,
                 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 g_unqualified.cpp:1:
/usr/include/c++/13/tuple:2201:5: note:   'std::swap'
 2201 |     swap(tuple<_Elements...>&, tuple<_Elements...>&) = delete;
      |     ^~~~
In file included from /usr/include/c++/13/bits/exception_ptr.h:41,
                 from /usr/include/c++/13/exception:164,
                 from /usr/include/c++/13/ios:41:
/usr/include/c++/13/bits/move.h:189:5: note:   'std::swap'
  189 |     swap(_Tp& __a, _Tp& __b)
      |     ^~~~
/usr/include/c++/13/bits/move.h:189:5: note:   'std::swap'
/usr/include/c++/13/bits/exception_ptr.h:230:5: note:   'std::__exception_ptr::swap'
  230 |     swap(exception_ptr& __lhs, exception_ptr& __rhs)
      |     ^~~~
In file included from /usr/include/c++/13/compare:37,
                 from /usr/include/c++/13/bits/char_traits.h:56,
                 from /usr/include/c++/13/ios:42:
/usr/include/c++/13/concepts:243:43: note:   'std::ranges::__cust::swap'
  243 |       inline constexpr __cust_swap::_Swap swap{};
      |                                           ^~~~
/usr/include/c++/13/concepts:183:35: note:   'std::ranges::__cust_swap::swap'
  183 |       template<typename _Tp> void swap(_Tp&, _Tp&) = delete;
      |                                   ^~~~

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::swap; adding includes does not bring the name out of namespace std. The suggestions are a mixed list - the first std::swap it points at is a deleted overload for tuples, not the general template - and the general template appears further down, at the bits/move.h line the second suggestion names. The qualifier is the fix either way.

The exact rule

  • std::swap is declared and defined by #include <utility> since C++11, and nowhere else by rule. In the sweep probed here, <limits>, <cstring>, <cstdint>, <cstddef>, <initializer_list>, <version>, <climits>, <cctype>, <new>, and <typeinfo> do not declare it, and neither does a file with no include at all.
  • 'swap' is not a member of 'std' means the <utility> include is missing and nothing else in the file pulled the name in - and, in the contexts probed here, GCC prints no note naming <utility> and no suggestion under it, even though it prints exactly that note for std::exchange and std::move in the same contexts.
  • In the sweep probed here, <type_traits>, <compare>, <concepts>, <ratio>, and <bit> declare the name without defining it: a file whose only include is one of those compiles to an object, and the link fails with an undefined reference to std::swap<int>. Adding <utility> fixes that failure too - it is the same missing include, reported by the linker instead of the compiler.
  • In the sweep probed here, nineteen other headers rescue std::swap transitively - among them <algorithm>, <vector>, <string>, and <iostream> - so a file whose only include is one of those compiles and runs the call without <utility>. That is this library version's behavior, not a guarantee - no header is required to pull std::swap in.
  • std::exchange and std::move are declared by the same #include <utility>: in the programs shown here, which include only <limits> beside the use, each gets its own is not a member of 'std' error when that include is missing, with a note naming <utility>.
  • std::swap_ranges and std::iter_swap are declared by #include <algorithm>: in the programs shown here, which include only <limits> beside the use, each gets its own is not a member of 'std' error when that include is missing, with no header note.
  • std::ranges::swap is declared by the same #include <utility> since C++20: with the include it swaps two values passed as arguments, and in the lean context probed here it gets the error 'std::ranges' has not been declared when that include is missing.
  • 'swap' was not declared in this scope for an unqualified swap use is a different defect: qualify the name as std::swap - 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::swap. If <utility> is not there, add it - that is the fix in the common case, and it holds whether the file failed at compile time, failed at link time, or happened to compile before through a transitive rescue.
  • Second, do not wait for a note naming the header. For this error, in the contexts probed here, GCC prints none for swap itself - take the fix from the name's home header, <utility>. The notes under the exchange and move errors name the same header, when those names are the ones failing.
  • Third, if the failure is an undefined reference at link time rather than a compile error, do not go looking for a library to link. In the probed set that shape is the same missing include, one step later: a header declared the template and none defined it, and <utility> is still the fix.
  • Fourth, do not be reassured by <iostream>, <vector>, or <string> being present, and do not remove <utility> because the file compiles without it. In the sweep here those headers rescue the name only transitively, and the rescue is not guaranteed.
  • Fifth, if the error says was not declared in this scope instead, fix the name's qualifier to std::swap as well.
  • Sixth, if the failing name is a neighboring one, split it by header before fixing: exchange and move take the same added <utility>, while swap_ranges and iter_swap take <algorithm>.

Which fix for which cause

  • #include <utility> missing, std::swap used in a file whose only include is <limits>, <cstring>, <cstdint>, <cstddef>, <initializer_list>, <version>, <climits>, <cctype>, <new>, or <typeinfo>, or that includes nothing: the message is 'swap' is not a member of 'std', with no header note and no suggestion - add #include <utility>.
  • std::swap used with only <type_traits>, <compare>, <concepts>, <ratio>, or <bit> included: there is no compile error - the link fails with an undefined reference to std::swap<int> - add #include <utility>.
  • std::swap used with only <algorithm>, <vector>, <string>, <iostream>, <memory>, <optional>, <array>, <numeric>, <functional>, <tuple>, <map>, <set>, <thread>, <mutex>, <chrono>, <stdexcept>, <exception>, <iterator>, or <system_error> included: no error on this library version - add #include <utility> anyway, because the rescue is not guaranteed.
  • std::exchange or std::move used without <utility> in the lean context: the message names that name, with a note naming <utility> - add #include <utility>.
  • std::swap_ranges or std::iter_swap used without <algorithm> in the lean context: the message names that name, with no header note - add #include <algorithm>.
  • std::ranges::swap used without <utility> in the lean context: the message is 'std::ranges' has not been declared - add #include <utility>.
  • swap used without the std:: qualifier, <utility> included: the message is 'swap' was not declared in this scope - qualify the name as std::swap.

Checklist

  • Check the include list of the file that names the swap, not just the project. The include must be in that file, or in a header that file includes directly by design.
  • Do not wait for a note naming the header. For this error, in the contexts probed here, GCC prints none for swap - take the fix from the name's home header, <utility>, instead.
  • Read a link failure for what it is: in the probed set, an undefined reference to std::swap<int> after a clean compile is the same missing <utility>, declared-but-not-defined - not a missing library.
  • Do not be reassured by the textbook includes being present, and do not delete a working <utility> line because a file compiles without it. In the sweep here the common headers rescue std::swap only transitively.
  • One header per name the file uses: std::vector comes from <vector> and std::string from <string>, even in a program whose subject is the swap - and std::swap comes from <utility> in return.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 2 1 for the swapped integers, beta alpha, 9 8, and 1 2 for the strings and the vectors, and 7 8 9 and 1 2 3 for the swapped arrays.

Sources

  • cppreference, std::swap — for std::swap being a function template defined in <utility> since C++11 and in <algorithm> before C++11, exchanging the given values, with the first overload swapping two values (participating in overload resolution only when the type is move-constructible and move-assignable, since C++17), the second overload swapping two arrays in effect by calling std::swap_ranges, and both overloads being constexpr since C++20.
  • cppreference, Standard library header <utility> — for <utility> being the header that declares std::swap, std::exchange, and std::move, and the std::ranges::swap customization point object since C++20.
  • cppreference, std::exchange — for std::exchange being a function template defined in <utility> since C++14 that replaces the value of its first argument with a new value and returns the argument's previous value.
  • cppreference, std::swap_ranges — for std::swap_ranges being a function template defined in <algorithm> that exchanges the elements of the range [first1, last1) with the elements of the range beginning at first2, returning an iterator past the last element exchanged in the second range.
  • cppreference, std::iter_swap — for std::iter_swap being a function template defined in <algorithm> that swaps the elements pointed to by two iterators.

Keep Exploring