C++
'std::pair' Is Not a Member of 'std' Fix
std::pair lives in
Written November 29, 2026 · Tested with GCC 13.3.0 · 8 min read
- The error: a header that declares std but not pair
- The same error for std::makepair
- Why the same code compiles in another file
- Neighboring headers: which cause it, which hide it
Written November 29, 2026
Tested with GCC 13.3.0
What this guide is
std::pair is a class template that bundles two values, of possibly different types, as one object with two public members, first and second. It and its factory function std::make_pair are declared in one header: <utility>.
This error appears in a translation unit where the std namespace itself has been declared - by almost any standard header - but no header in the file has declared pair. The compiler knows the namespace, looks the name up in it, and reports that the name is not a member. Whether a given file hits the error depends entirely on which headers it happens to include, because on a real standard library many headers declare pair on the side, for their own internal use. This guide reproduces the message, maps which probed headers cause it and which hide it, and gives the fix.
The error: a header that declares std but not pair
This program declares a pair of int values and includes only <cstddef> - a header that declares namespace-std names such as std::size_t, but not pair:
#include <cstddef>
int main() {
std::pair<int, int> p{1, 2};
return p.first + p.second;
}
v_cstddef.cpp: In function ‘int main()’:
$ g++ -std=c++20 v_cstddef.cpp -o v_cstddef_prog
v_cstddef.cpp: In function ‘int main()’:
v_cstddef.cpp:3:10: error: ‘pair’ is not a member of ‘std’
3 | std::pair<int, int> p{1, 2};
| ^~~~
v_cstddef.cpp:2:1: note: ‘std::pair’ is defined in header ‘<utility>’; did you forget to ‘#include <utility>’?
1 | #include <cstddef>
+++ |+#include <utility>
2 | int main() {
v_cstddef.cpp:3:15: error: expected primary-expression before ‘int’
3 | std::pair<int, int> p{1, 2};
| ^~~
v_cstddef.cpp:4:12: error: ‘p’ was not declared in this scope
4 | return p.first + p.second;
| ^
Read the note under the error: GCC states the diagnosis itself - 'std::pair' is defined in header '<utility>'; did you forget to '#include <utility>'? The errors that follow (expected primary-expression before 'int', 'p' was not declared) are knock-on effects of the first one; the declaration failed, so the variable does not exist for the lines below it.
The same first error appears with <cstdio>, <limits>, or <type_traits> as the file's only header, and in a file with no includes at all. What those header sets have in common is only that none of them declares pair.
The same error for std::make_pair
make_pair lives in the same header, so it fails the same way in the same kind of file:
#include <cstddef>
int main() {
auto p = std::make_pair(1, 2);
return p.first + p.second;
}
s_makepair.cpp: In function ‘int main()’:
$ g++ -std=c++20 s_makepair.cpp -o s_makepair_prog
s_makepair.cpp: In function ‘int main()’:
s_makepair.cpp:3:19: error: ‘make_pair’ is not a member of ‘std’
3 | auto p = std::make_pair(1, 2);
| ^~~~~~~~~
s_makepair.cpp:2:1: note: ‘std::make_pair’ is defined in header ‘<utility>’; did you forget to ‘#include <utility>’?
1 | #include <cstddef>
+++ |+#include <utility>
2 | int main() {
Here the message names make_pair: 'make_pair' is not a member of 'std'. One include declares both names, because both are <utility>'s contents.
Why the same code compiles in another file
Move the pair code into a file that prints its result, and the error is gone - with no <utility> anywhere:
#include <iostream>
#include <string>
int main() {
std::pair<int, std::string> p = std::make_pair(1, std::string("one"));
std::cout << p.first << " " << p.second << "\n";
return 0;
}
COMPILE_OK
$ ./a_noinclude_prog
1 one
This program includes only <iostream> and <string>, and it compiles and prints 1 one. On this standard library, both of those headers declare std::pair in full as a side effect of their own internals. That is the whole mystery of this error: it is not about the pair code, which is identical in both files, but about whether some header in the file happened to declare the name first. Adding #include <iostream> to a failing file to debug it makes the error disappear, and removing an unused-looking include from a working file can make it appear - both are the same mechanism, a declaration that was only ever there transitively. A transitive declaration is an implementation detail of the library version in use, not something the standard promises; the header a name is specified in is the only include that guarantees it.
Neighboring headers: which cause it, which hide it
Probed one at a time as a file's only header (or only extra header), on this library:
Headers that leave the error in place - they declare std names but not pair:
<cstddef><cstdio><cstdint><cstring><limits><iosfwd><type_traits><exception><new><cctype><cstdlib>
Headers that hide it, by declaring pair transitively on this library:
<iostream><string><ostream><cmath><map><vector><tuple><memory><algorithm><functional><unordered_map><array><list><set>
Both lists are observations about one library implementation, and either can change between library versions. They explain the error's behavior; they are not a menu of fixes. The fix is the header from the first list's opposite number - the one that declares the name by specification.
The fix
Include <utility>. In the minimal file, that is the only change:
#include <cstddef>
#include <cstdio>
#include <utility>
int main() {
std::pair<int, int> p = std::make_pair(1, 2);
std::printf("%d %d\n", p.first, p.second);
return 0;
}
COMPILE_OK
$ ./s_fixed_prog
1 2
The output is 1 2, the pair's two members. (<cstdio> is in this program only to print the result; the probed sweep above shows it does not declare pair, so the fix visible here is <utility> alone.) With ordinary printing headers the fixed program is the familiar shape:
#include <iostream>
#include <string>
#include <utility>
int main() {
std::pair<int, std::string> p{7, "seven"};
std::cout << p.first << " " << p.second << "\n";
p.first = 8;
std::cout << p.first << " " << p.second << "\n";
return 0;
}
COMPILE_OK
$ ./g_fixed_prog
7 seven
8 seven
The output is 7 seven, then 8 seven after p.first is assigned a new value: first and second are ordinary public data members, read and written directly, with no accessor call.
make_pair and structured bindings
std::make_pair deduces the pair's member types from its arguments, so the types are written once, in the values:
#include <iostream>
#include <map>
#include <string>
#include <utility>
int main() {
auto p = std::make_pair(3, std::string("three"));
std::cout << p.first << " " << p.second << "\n";
std::map<std::string, int> m = {{"ana", 3}};
for (const auto& [key, value] : m) std::cout << key << " " << value << "\n";
return 0;
}
COMPILE_OK
$ ./h_fixed_prog
3 three
ana 3
The output is 3 three for the made pair, then ana 3: iterating a map yields each entry as a pair - specifically a std::pair<const std::string, int> - and the structured binding auto& [key, value] names its two members directly. Structured bindings work on any pair, including one declared on its own:
#include <iostream>
#include <string>
#include <utility>
int main() {
std::pair<int, std::string> p{42, "answer"};
auto [num, word] = p;
std::cout << num << " " << word << "\n";
return 0;
}
COMPILE_OK
$ ./b_fixed_prog
42 answer
The output is 42 answer - the binding copies the pair and gives its members the names num and word for the rest of the scope.
Pairs in a map and a vector
Pairs are the currency of the associative containers: a map's entries are pairs, and inserting one accepts either spelling - a made pair or a pair value:
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>
int main() {
std::map<std::string, int> scores;
scores.insert(std::make_pair(std::string("ana"), 3));
scores.insert(std::pair<std::string, int>("bo", 5));
for (const auto& kv : scores) std::cout << kv.first << " " << kv.second << "\n";
std::vector<std::pair<std::string, int>> v = {{"pen", 3}, {"book", 12}};
for (const auto& pr : v) std::cout << pr.first << " " << pr.second << "\n";
return 0;
}
COMPILE_OK
$ ./c_fixed_prog
ana 3
bo 5
pen 3
book 12
The output is ana 3 and bo 5 (the map, ordered by key), then pen 3 and book 12 (the vector, in insertion order). A std::vector<std::pair<...>> is the standard way to keep an ordered list of key-value entries when map ordering or lookup is not wanted.
Comparing and sorting pairs
Pairs compare lexicographically: first members decide, and second members break the tie. That single rule makes a vector of pairs sortable with no custom comparison:
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
int main() {
std::vector<std::pair<int, std::string>> v = {{2, "two"}, {1, "one"}, {2, "apple"}};
std::sort(v.begin(), v.end());
for (const auto& pr : v) std::cout << pr.first << " " << pr.second << "\n";
return 0;
}
COMPILE_OK
$ ./i_fixed_prog
1 one
2 apple
2 two
The output is 1 one, 2 apple, 2 two: sorted by the int first, and the two entries that tie on 2 are ordered by their strings. The comparisons themselves are ordinary operators on pair values:
#include <iostream>
#include <utility>
int main() {
std::pair<int, int> a{1, 5};
std::pair<int, int> b{1, 7};
std::pair<int, int> c{2, 0};
std::cout << (a < b) << "\n";
std::cout << (a < c) << "\n";
std::cout << (a == std::pair<int, int>{1, 5}) << "\n";
return 0;
}
COMPILE_OK
$ ./d_fixed_prog
1
1
1
The output is 1, 1, 1: (1, 5) is less than (1, 7) on the second member, less than (2, 0) on the first, and equal to another (1, 5).
Swapping and nesting pairs
A pair can exchange its contents with another pair of the same type in one call, and a pair's member can itself be a pair:
#include <iostream>
#include <string>
#include <utility>
int main() {
std::pair<int, int> x{1, 2};
std::pair<int, int> y{9, 8};
x.swap(y);
std::cout << x.first << " " << x.second << "\n";
std::pair<std::string, std::pair<int, int>> nested{"point", {3, 4}};
std::cout << nested.first << " " << std::get<0>(nested.second) << " " << std::get<1>(nested.second) << "\n";
return 0;
}
COMPILE_OK
$ ./e_fixed_prog
9 8
point 3 4
The output is 9 8 - the values x received from y in the swap - then point 3 4: the nested pair is reached as nested.second, and std::get<0> and std::get<1> read its members by index, the same indices that name the members of any pair or tuple.
Traps
The same missing declaration in a different position is worded differently. With std::pair as a function's return type, GCC does not say "not a member":
#include <cstddef>
std::pair<int, int> make() { return {1, 2}; }
int main() {
auto p = make();
return p.first + p.second;
}
s_cstddef.cpp:2:6: error: ‘pair’ in namespace ‘std’ does not name a template type
$ g++ -std=c++20 s_cstddef.cpp -o s_cstddef_prog
s_cstddef.cpp:2:6: error: ‘pair’ in namespace ‘std’ does not name a template type
2 | std::pair<int, int> make() { return {1, 2}; }
| ^~~~
s_cstddef.cpp:2:1: note: ‘std::pair’ is defined in header ‘<utility>’; did you forget to ‘#include <utility>’?
1 | #include <cstddef>
+++ |+#include <utility>
2 | std::pair<int, int> make() { return {1, 2}; }
s_cstddef.cpp: In function ‘int main()’:
s_cstddef.cpp:4:14: error: ‘make’ was not declared in this scope
4 | auto p = make();
| ^~~~
The message is 'pair' in namespace 'std' does not name a template type. It is the same defect - no header has declared the template - reported by a different lookup path, and the same include fixes it.
Dropping the std:: qualification fails even with <utility> included:
#include <iostream>
#include <utility>
int main() {
pair<int, int> p{1, 2};
std::cout << p.first << "\n";
return 0;
}
u_unqual.cpp: In function ‘int main()’:
$ g++ -std=c++20 u_unqual.cpp -o u_unqual_prog
u_unqual.cpp: In function ‘int main()’:
u_unqual.cpp:4:5: error: ‘pair’ was not declared in this scope; did you mean ‘std::pair’?
4 | pair<int, int> p{1, 2};
| ^~~~
| std::pair
In file included from /usr/include/c++/13/string:48,
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 u_unqual.cpp:1:
/usr/include/c++/13/bits/stl_iterator.h:2992:12: note: ‘std::pair’ declared here
2992 | struct pair;
| ^~~~
u_unqual.cpp:4:10: error: expected primary-expression before ‘int’
4 | pair<int, int> p{1, 2};
| ^~~
u_unqual.cpp:5:18: error: ‘p’ was not declared in this scope
5 | std::cout << p.first << "\n";
| ^
There is no name pair at the top level; GCC's suggestion, did you mean 'std::pair'?, points at the qualified name the program needed.
And there is no language-mode version of this trap: pair is an old facility, not a recent one. A plain pair program compiled in C++11 mode, with <utility> included, works unchanged:
#include <iostream>
#include <string>
#include <utility>
int main() {
std::pair<int, std::string> p{42, "answer"};
std::cout << p.first << " " << p.second << "\n";
return 0;
}
COMPILE_OK
$ ./u_cpp11_prog
42 answer
What does depend on the mode is the syntax around pairs, not the type: structured bindings are a C++17 feature, so a program that unpacks a pair with auto [a, b] is written for a C++17-or-later mode - GCC 13.3 still compiles that line in C++14 mode, but only as an extension, with a warning that structured bindings are only available with -std=c++17 or later - while the pair itself compiles in every standard mode.
Related guides on this site
- 'std::unique' Is Not a Member of 'std' Fix
- 'std::search_n' Is Not a Member of 'std' Fix
- 'std::replace' Is Not a Member of 'std' Fix
- 'std::remove' Is Not a Member of 'std' Fix
Sources
- cppreference,
<utility>header - what it supports: the header that declaresstd::pairandstd::make_pair. - cppreference,
std::pair- what it supports: the class template in<utility>, itsfirstandsecondmembers, its comparison operators,swap, andstd::get. - cppreference,
std::make_pair- what it supports: the factory function in<utility>that deduces a pair's member types from its arguments.
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.