C++

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

std::max lives in one header, <algorithm> - but many common headers pull it in on the side, so the error only shows up in lean files. A file that prints with std::cout compiles without it, because <iostream> rescues the name. 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 · 11 min read

By the end of this guide
  • The error: the include is missing
  • The textbook version does not fail - a common header rescues it
  • Neighboring headers: which ones rescue it, which ones do not
  • The fix

Written November 30, 2026

Tested with GCC 13.3.0

What this guide is

std::max is a function template defined in the header <algorithm>. It returns the greater of the two values it is given, using operator< - or, in its initializer-list form, the greatest value in the list.

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

  • The #include <algorithm> line is missing, and no other header in the file pulls the name in transitively. For this error in a lean file GCC prints no note naming <algorithm> and no suggestion, so the fix has to come from knowing the name's home, not from reading a note.

What makes this error easy to miss is that many ordinary files never see it: <iostream>, <vector>, <string>, and several other common headers do pull the name in on this library version, so a file can use std::max for a long time without ever including its home header - until the includes change and the lean context is exposed. Each case is separated below.

The error: the include is missing

This program compares 5 and 2 with std::max, but includes only <type_traits>:

C++
#include <type_traits>
int main() {
    return std::max(5, 2) == 5 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 a_type_traits.cpp -o a_prog
a_type_traits.cpp: In function 'int main()':
a_type_traits.cpp:3:17: error: 'max' is not a member of 'std'
    3 |     return std::max(5, 2) == 5 ? 0 : 1;
      |                 ^~~

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() {
    return std::max(5, 2) == 5 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:2:17: error: 'max' is not a member of 'std'
    2 |     return std::max(5, 2) == 5 ? 0 : 1;
      |                 ^~~

The initializer-list form fails the same way in the same lean context: std::max({5, 2, 8}) with only <type_traits> included gets the same 'max' is not a member of 'std' error at the same position. Every overload of the name lives behind the same missing include.

The textbook version does not fail - a common header rescues it

The version most often written first - print the larger of two values with std::cout - compiles without <algorithm>:

C++
#include <iostream>
int main() {
    std::cout << std::max(3, 7) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 t_textbook.cpp -o t_prog
$ ./t_prog
7

What happened: <iostream> pulls the name in transitively on this library version, so the call compiles and prints 7, the greater of 3 and 7. Adding <vector> changes nothing - a file with <iostream> and <vector> that prints the larger of two vector elements also compiles without <algorithm> and prints 8 for the elements 5 and 8. That rescue is a property of this library version, not a rule of the language - no header except <algorithm> is required to declare std::max, and another library could leave it out. A program that uses std::max includes <algorithm>, whether or not it happens to compile without it.

Neighboring headers: which ones rescue it, which ones do not

In a sweep where each header below was compiled as the only header in a program that returns std::max(5, 2), the outcome splits. These headers fail with the plain is not a member of 'std' error, with no note and no suggestion:

  • <type_traits>
  • <utility>
  • <numeric>
  • <limits>
  • <mutex>
  • <optional>
  • <cstring>

A file with no include at all fails the same way. These probed headers rescue the program - it compiles with no <algorithm> anywhere in the file:

  • <vector>
  • <string>
  • <iostream>
  • <functional>
  • <array>
  • <cmath>

The same holds for the other failing contexts in the sweep. This program includes only <utility>:

C++
#include <utility>
int main() {
    return std::max(5, 2) == 5 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 a_utility.cpp -o a_utility_prog
a_utility.cpp: In function 'int main()':
a_utility.cpp:3:17: error: 'max' is not a member of 'std'
    3 |     return std::max(5, 2) == 5 ? 0 : 1;
      |                 ^~~

And this one includes only <limits>:

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

The fix

Add the header:

C++
#include <algorithm>
#include <iostream>
int main() {
    std::cout << std::max(5, 2) << "\n";
    std::cout << std::max(3, 7) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 h_fixed.cpp -o h_prog
$ ./h_prog
5
7

One include, <algorithm>, declares the template. std::max(5, 2) returns the greater of its two arguments, so the program prints 5; std::max(3, 7) prints 7. If the two values are equal, std::max returns the first of them.

The neighboring names: the whole header family fails too

The other names declared by <algorithm> fail with the same shape of error in the same lean context. std::minmax - the function template that returns a pair holding the lesser and the greater of two values - gets its own error, again with no header note:

C++
#include <type_traits>
int main() {
    auto p = std::minmax(5, 2);
    return p.first == 2 && p.second == 5 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 b_minmax.cpp -o b_prog
b_minmax.cpp: In function 'int main()':
b_minmax.cpp:3:19: error: 'minmax' is not a member of 'std'
    3 |     auto p = std::minmax(5, 2);
      |                   ^~~~~~

std::clamp - the function template that returns the value in a range [lo, hi] closest to the given value - fails the same way:

C++
#include <type_traits>
int main() {
    return std::clamp(10, 0, 5) == 5 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 c_clamp.cpp -o c_prog
c_clamp.cpp: In function 'int main()':
c_clamp.cpp:3:17: error: 'clamp' is not a member of 'std'
    3 |     return std::clamp(10, 0, 5) == 5 ? 0 : 1;
      |                 ^~~~~

And std::min, the smaller-of-two counterpart of std::max in the same header, fails the same way in this lean context:

C++
#include <type_traits>
int main() {
    return std::min(5, 2) == 2 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 d_min.cpp -o d_prog
d_min.cpp: In function 'int main()':
d_min.cpp:3:17: error: 'min' is not a member of 'std'
    3 |     return std::min(5, 2) == 2 ? 0 : 1;
      |                 ^~~

One added include declares every one of these names: the greater-of-two, the pair of both, the bounded value, and the smaller-of-two.

The neighboring names, fixed

With <algorithm> added, std::max works on more than two plain ints. The larger of the vector elements 5 and 8 prints 8, and of the two strings "veldica" and "zuck" it prints zuck, the one that compares greater:

C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
    std::vector<int> values = {5, 2, 8};
    std::cout << std::max(values[0], values[2]) << "\n";
    std::string a = "veldica";
    std::string b = "zuck";
    std::cout << std::max(a, b) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 i_string_vector.cpp -o i_prog
$ ./i_prog
8
zuck

And std::minmax(5, 2) returns the pair 2 5 - lesser first, greater second - while std::clamp holds 10 down to the upper bound 5 and lifts -3 up to the lower bound 0:

C++
#include <algorithm>
#include <iostream>
int main() {
    auto p = std::minmax(5, 2);
    std::cout << p.first << " " << p.second << "\n";
    std::cout << std::clamp(10, 0, 5) << "\n";
    std::cout << std::clamp(-3, 0, 5) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 j_minmax_clamp.cpp -o j_prog
$ ./j_prog
2 5
5
0

A different wording: the name without its qualifier

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

C++
#include <algorithm>
#include <iostream>
int main() {
    std::cout << max(5, 2) << "\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:18: error: 'max' was not declared in this scope
    4 |     std::cout << max(5, 2) << "\n";
      |                  ^~~
g_unqualified.cpp:4:18: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:61,
                 from g_unqualified.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:5805:5: note:   'std::max'
 5805 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:2928:29: note:   'std::ranges::max'
 2928 |   inline constexpr __max_fn max{};
      |                             ^~~

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::max; adding includes does not bring the name out of namespace std. Note that the std::max the suggestion points at is the initializer-list overload declared at that line, not the two-value overload the call needs - the suggestion names the namespace, and the qualifier is the fix either way.

The exact rule

  • std::max is declared by #include <algorithm>, and nowhere else by rule. In the sweep probed here, <type_traits>, <utility>, <numeric>, <limits>, <mutex>, <optional>, and <cstring> do not declare it, and neither does a file with no include at all.
  • 'max' is not a member of 'std' means the <algorithm> include is missing and nothing else in the file pulled the name in - and, in the contexts probed here, GCC prints no note naming <algorithm> and no suggestion under it.
  • In the sweep probed here, <vector>, <string>, <iostream>, <functional>, <array>, and <cmath> rescue std::max transitively: a file whose only include is one of those headers compiles the call without <algorithm>. That is this library version's behavior, not a guarantee - no header is required to pull std::max in.
  • The initializer-list form std::max({5, 2, 8}) is declared by the same #include <algorithm>: in the lean context probed here it gets the same is not a member of 'std' error when that include is missing.
  • std::minmax, std::clamp, and std::min are declared by the same #include <algorithm>: in the programs shown here, which include only <type_traits> beside the use, each gets its own is not a member of 'std' error when that include is missing, with no header note.
  • 'max' was not declared in this scope for an unqualified max use is a different defect: qualify the name as std::max - 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::max. If <algorithm> is not there, add it - that is the fix in the common case, and it holds whether or not the file happened to compile before through a transitive rescue.
  • Second, do not be reassured by the file compiling today. In the sweep here <vector>, <string>, <iostream>, <functional>, <array>, and <cmath> all rescue the name - and none of them is required to, so a file that uses std::max includes <algorithm> itself.
  • Third, do not wait for a note naming the header. For this error, in the contexts probed here, GCC prints none - take the fix from the name's home header, <algorithm>.
  • Fourth, if the error says was not declared in this scope instead, fix the name's qualifier to std::max as well.
  • Fifth, if the failing name is a neighboring one - minmax, clamp, or min - the same added include fixes it: every one of those names shares the header, and none of them gets a header note in the programs here either.

Which fix for which cause

  • #include <algorithm> missing, std::max used in a file whose only include is <type_traits>, <utility>, <numeric>, <limits>, <mutex>, <optional>, or <cstring>, or that includes nothing: the message is 'max' is not a member of 'std', with no header note and no suggestion - add #include <algorithm>.
  • std::max used with only <vector>, <string>, <iostream>, <functional>, <array>, or <cmath> included: no error on this library version - add #include <algorithm> anyway, because the rescue is not guaranteed.
  • std::minmax, std::clamp, or std::min used without <algorithm> in the lean context: the message names that name, with no header note - add #include <algorithm>.
  • max used without the std:: qualifier, <algorithm> included: the message is 'max' was not declared in this scope - qualify the name as std::max.

Checklist

  • Check the include list of the file that names the comparison, 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 - take the fix from the name's home header, <algorithm>, instead.
  • Do not be reassured by <iostream> or <vector> being present. In the sweep here both rescue std::max transitively, but neither is its home header - a change that removes either include can expose the missing <algorithm> the same day.
  • 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 comparison - and std::max comes from <algorithm> in return.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 5 and 7 for the two int comparisons, 8 and zuck for the vector-element and string comparisons, and 2 5, 5, and 0 for the pair and the two clamps.

Sources

  • cppreference, std::max — for std::max being a function template defined in <algorithm> that returns the greater of two given values using operator<, returns the first of them if they are equivalent, and in its initializer-list form returns the greatest value in the list.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header that declares the std::max, std::min, std::minmax, and std::clamp function templates.
  • cppreference, std::minmax — for std::minmax being a function template defined in <algorithm> that returns a pair holding the lesser and the greater of two values, in that order.
  • cppreference, std::clamp — for std::clamp being a function template defined in <algorithm>, since C++17, that returns the value in [lo, hi] closest to the given value: lo if the value is less than lo, hi if hi is less than the value, and the value itself otherwise.

Keep Exploring