C++

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

std::clamp lives in one header, <algorithm> - and on this library version almost no ordinary file rescues it by accident: in the sweep probed here only <functional> and <chrono> pull the name in on the side, and the textbook combination of <iostream> and <vector> does not. The error therefore surfaces in textbook files as well as lean ones, and in the sweep probed here it splits only two ways - the plain error, or one of those two transitive rescues - with no link-failure shape behind any probed header. This guide pins down both 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 · 13 min read

By the end of this guide
  • The error: the include is missing, in a lean file
  • A neighboring header does not rescue it: only &lt;utility&gt;
  • The textbook version fails too - the common headers do not rescue it
  • Neighboring headers: which ones fail, which ones rescue it

Written December 1, 2026

Tested with GCC 13.3.0

What this guide is

std::clamp is a function template defined in the header <algorithm>, since C++17. It takes a value v and two boundaries lo and hi and returns a reference to the value in [lo, hi] that is closest to v: lo if v is less than lo, hi if hi is less than v, and v itself otherwise. A second overload takes a comparator comp and orders by comp instead of operator< (by std::less{} since C++20). Both overloads are constexpr since C++17, each makes at most two comparisons, and if lo is greater than hi under the ordering in use the behavior is undefined. Because the result is a reference, capturing it by reference from a call whose arguments include a temporary can leave a dangling reference; the programs here copy the result by value.

Because the template lives in one specific header, 'clamp' 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 the contexts probed here, 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. The neighboring names probed here - std::min and std::max, both in the same header - are silent the same way, as the neighboring-names section shows.

What makes this error worth separating from its relatives is the shape of the sweep behind it. In the sweep probed here the outcome splits only two ways - the plain compile error, or a transitive rescue - and the rescue side is unusually small: only two probed headers besides <algorithm> itself rescue this name, where the neighboring std::lower_bound sweep was rescued by thirteen. The older siblings in this family - std::sort, std::min, and std::max - have their own guides and are linked at the end, not re-taught here.

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

This program clamps 5 into the range 0 to 10 with std::clamp, but includes only <limits>:

C++
#include <limits>
int main() {
    return std::clamp(5, 0, 10) == 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: 'clamp' is not a member of 'std'
    3 |     return std::clamp(5, 0, 10) == 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::clamp(5, 0, 10) == 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: 'clamp' is not a member of 'std'
    2 |     return std::clamp(5, 0, 10) == 5 ? 0 : 1;
      |                 ^~~~~

A neighboring header does not rescue it: only <utility>

A reader coming from the std::swap error might reach for <utility> first. In the program probed here it changes nothing - the same plain error, at the same position:

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

That is expected once the homes are separated: <utility> declares std::swap, and std::clamp is not one of its names. The two errors share a shape and a sentence pattern, but each name takes its own header, and the fix below is <algorithm>, not the swap fix.

The textbook version fails too - the common headers do not rescue it

The version most often written first - clamp each element of a vector into a range and print the results with std::cout - gets the same error, without <algorithm>:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {-3, 5, 42};
    for (int x : v) std::cout << std::clamp(x, 0, 10) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:39: error: 'clamp' is not a member of 'std'
    5 |     for (int x : v) std::cout << std::clamp(x, 0, 10) << "\n";
      |                                       ^~~~~

What happened: on this library version, neither <vector> nor <iostream> pulls this name in transitively, so the call fails in a textbook file just as it does in a lean one. That is the opposite of the neighboring names in this family, whose textbook files are rescued - do not import their behavior into this error. In the sweep probed here, the only headers that rescue std::clamp are <functional> and <chrono>, besides <algorithm> itself, and that rescue is a property of this library version, not a rule of the language. A program that uses std::clamp includes <algorithm>, whether or not it happens to compile without it.

Neighboring headers: which ones fail, which ones rescue it

In a sweep where each header below was compiled as the only header in a program that clamps 5 into the range 0 to 10 and returns whether the result is 5, the outcome splits two 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>
  • <type_traits>
  • <compare>
  • <concepts>
  • <ratio>
  • <bit>
  • <optional>
  • <numeric>
  • <tuple>
  • <mutex>
  • <exception>
  • <utility>
  • <vector>
  • <string>
  • <iostream>
  • <memory>
  • <array>
  • <map>
  • <set>
  • <thread>
  • <stdexcept>
  • <iterator>
  • <system_error>

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

  • <algorithm>
  • <functional>
  • <chrono>

Two contrasts with the neighboring sweeps are worth holding on to. First, the split here is two-way: in the sweep probed here, every failing sole header fails at compile time with the plain error - each one was also compiled to an object file on its own, and that step failed the same way - so the declared-but-not-defined link failure that std::swap produces behind five headers has no counterpart for this name in the probed set. Second, the rescue list is far shorter than its relatives': <vector>, <string>, <iostream>, <memory>, <array>, <map>, <set>, <thread>, <stdexcept>, <iterator>, and <system_error> rescued std::lower_bound in its sweep and fail here, while <algorithm> - the home header - is on this list by definition, not by transitivity. Every other rescue on that list is this library version's behavior, not a guarantee.

The fix

Add the header:

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

One include, <algorithm>, declares the template. std::clamp(5, 0, 10) returns 5, the value itself, because 5 already lies inside the range, so the program prints 5.

The fixed programs: below, above, and inside the range

With <algorithm> added, std::clamp handles the three positions a value can sit in. Clamping -3 into 0 to 10 returns the lower boundary 0, clamping 42 returns the upper boundary 10, and clamping 5 returns 5 itself:

C++
#include <algorithm>
#include <iostream>
int main() {
    std::cout << std::clamp(-3, 0, 10) << " " << std::clamp(42, 0, 10) << " " << std::clamp(5, 0, 10) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 i_cases.cpp -o i_cases_prog
$ ./i_cases_prog
0 10 5

The comparator overload orders by the comparator instead of operator<. With std::greater<int> the ordering is reversed, so the boundaries are written in the reversed order - 10 first, then 0 - and the same three positions come back under that ordering: 5 stays 5, the value 15, which sits past the 10 end under the reversed ordering, returns 10, and -3, past the 0 end, returns 0:

C++
#include <algorithm>
#include <functional>
#include <iostream>
int main() {
    std::cout << std::clamp(5, 10, 0, std::greater<int>()) << " " << std::clamp(15, 10, 0, std::greater<int>()) << " " << std::clamp(-3, 10, 0, std::greater<int>()) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 j_comp.cpp -o j_comp_prog
$ ./j_comp_prog
5 10 0

The template is not limited to integers. Clamping a double into 0.0 to 1.5 returns 1.5 for the value 2.5 above the range, 0 for -0.5 below it - printed without a fractional part, as the default stream formatting renders 0.0 - and 0.75 for the value already inside:

C++
#include <algorithm>
#include <iostream>
int main() {
    std::cout << std::clamp(2.5, 0.0, 1.5) << " " << std::clamp(-0.5, 0.0, 1.5) << " " << std::clamp(0.75, 0.0, 1.5) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_float.cpp -o k_float_prog
$ ./k_float_prog
1.5 0 0.75

The neighboring names: same header, same silence

The two names that std::clamp is usually built from by hand share its home header and its silence. std::min - the function template defined in <algorithm> that returns the smaller of its two arguments - gets its own error, again with no header note:

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

std::max - the function template defined in <algorithm> that returns the greater of its two arguments - fails the same way:

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

One contrast is worth holding on to: in the programs here, clamp, min, and max all get no note. Do not read the absence of a note as a puzzle to solve - in this probed set the note is simply absent for this header's names, and the fix comes from the names' home, <algorithm>.

The neighboring names, fixed

And the neighboring names work under the same one added header: std::min of 3 and 7 is 3, and std::max is 7:

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

Since C++20 the same <algorithm> header also declares std::ranges::clamp, the ranges form of the same operation. With <algorithm> included it clamps 42 into 0 to 10 and returns 10; 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 <algorithm>
#include <iostream>
int main() {
    std::cout << std::ranges::clamp(42, 0, 10) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
10
C++
#include <limits>
int main() {
    return std::ranges::clamp(5, 0, 10) == 5 ? 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:3:17: error: 'std::ranges' has not been declared
    3 |     return std::ranges::clamp(5, 0, 10) == 5 ? 0 : 1;
      |                 ^~~~~~

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 << clamp(5, 0, 10) << "\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:4:18: error: 'clamp' was not declared in this scope
    4 |     std::cout << clamp(5, 0, 10) << "\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:3687:5: note:   'std::clamp'
 3687 |     clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi, _Compare __comp)
      |     ^~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:2952:31: note:   'std::ranges::clamp'
 2952 |   inline constexpr __clamp_fn clamp{};
      |                               ^~~~~

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::clamp; adding includes does not bring the name out of namespace std. Here the suggestions do name the right names - the first points at std::clamp itself, in the internal header the transcript names, and the second at std::ranges::clamp - and the qualifier is still the fix either way.

The exact rule

  • std::clamp is declared by #include <algorithm>, and by no other header by rule. In the sweep probed here, <limits>, <cstring>, <cstdint>, <cstddef>, <initializer_list>, <version>, <climits>, <cctype>, <new>, <typeinfo>, <type_traits>, <compare>, <concepts>, <ratio>, <bit>, <optional>, <numeric>, <tuple>, <mutex>, <exception>, <utility>, <vector>, <string>, <iostream>, <memory>, <array>, <map>, <set>, <thread>, <stdexcept>, <iterator>, and <system_error> do not declare it, and neither does a file with no include at all.
  • 'clamp' 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, the split is two-way: a sole header either fails with that plain compile error or rescues the program. Each failing sole header also failed when compiled to an object file on its own, so the probed set contains no declared-but-not-defined link-failure shape for this name.
  • In the sweep probed here, only two headers besides <algorithm> rescue std::clamp transitively - <functional> and <chrono> - so a file whose only include is one of those compiles and runs the call without <algorithm>. That is this library version's behavior, not a guarantee - in the probed set, those headers are not required to pull std::clamp in.
  • std::min and std::max are declared by the same #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::clamp is declared by the same #include <algorithm> since C++20: with the include it clamps the value passed to it, and in the lean context probed here it gets the error 'std::ranges' has not been declared when that include is missing.
  • 'clamp' was not declared in this scope for an unqualified clamp use is a different defect: qualify the name as std::clamp - 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::clamp. If <algorithm> is not there, add it - that is the fix in the common case, and it holds whether the file failed at compile 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 clamp itself - take the fix from the name's home header, <algorithm>. The neighboring names are silent the same way when they are the ones failing.
  • Third, do not reach for <utility> because the error sentence looks like the std::swap error. In the program probed here, <utility> as the only include fails with the same plain error - the two names live in different headers, and each takes its own.
  • Fourth, do not be reassured by <iostream>, <vector>, or <string> being present - in the sweep here none of them rescues this name at all - and do not remove <algorithm> because a file compiles without it: in the probed set only <functional> and <chrono> rescue it, and that rescue is not guaranteed.
  • Fifth, if the error says was not declared in this scope instead, fix the name's qualifier to std::clamp as well.
  • Sixth, if the failing name is a neighboring one, check its header before fixing: min and max take the same added <algorithm>, while swap takes <utility>.

Which fix for which cause

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

Checklist

  • Check the include list of the file that names the clamp, 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 clamp - take the fix from the name's home header, <algorithm>, instead.
  • Do not import the std::swap fix into this error: in the probed set, <utility> fails here the same plain way as <limits> does. Split neighboring names by home header first.
  • Do not assume the textbook includes rescue this name, and do not delete a working <algorithm> line because a file compiles without it. In the sweep here <iostream> and <vector> do not rescue std::clamp at all, and the two headers that do rescue it do so 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 clamp - and std::clamp comes from <algorithm> in return.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 5 for the value inside the range, 0 10 5 for the below, above, and inside cases, and 1.5 0 0.75 for the floating-point cases.

Sources

  • cppreference, std::clamp — for std::clamp being a function template defined in <algorithm> since C++17 that returns a reference to the value in [lo, hi] closest to v - lo if v is less than lo, hi if hi is less than v, otherwise v - with one overload ordering by operator< (by std::less{} since C++20) and a second overload ordering by a comparator comp, both overloads constexpr since C++17, at most two comparisons, undefined behavior if lo is greater than hi, and the note that capturing the result by reference from a call with a temporary argument can leave a dangling reference.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header that declares std::clamp and the neighboring algorithm names, including the std::ranges::clamp algorithm function object since C++20.
  • cppreference, std::min — for std::min being a function template defined in <algorithm> that returns the smaller of its two arguments.
  • cppreference, std::max — for std::max being a function template defined in <algorithm> that returns the greater of its two arguments.

Keep Exploring