C++
'std::ranges::sort' Is Not a Member Fix
The ranges algorithms do not live in
Written November 29, 2026 · Tested with GCC 13.3.0 · 5 min read
- The error: no algorithm header
- The fix that is not: adding <ranges>
- Neighboring headers: six fail, one works
- The fix
Written November 29, 2026
Tested with GCC 13.3.0
What this guide is
The ranges library arrived in C++20 in two halves that live in two different headers. The constrained algorithms - std::ranges::sort, std::ranges::find_if, std::ranges::copy, and the rest of the std::ranges versions of the classic algorithms - are declared in <algorithm>, the same header that declares their iterator-pair originals; std::ranges::sort lists <algorithm> as its header. The views - std::views::filter, std::views::transform, std::views::iota, and the range adaptors behind them - are declared in <ranges>.
That split is what makes this error confusing. A program that calls std::ranges::sort without <algorithm> fails with 'sort' is not a member of 'std::ranges' - the namespace exists, because other headers declare it, but the name inside it does not. And the fix the error seems to ask for, adding <ranges>, does not work: <ranges> never declares the algorithms. This guide reproduces the message, separates the headers that fail from the one that works, and gives the fix.
The error: no algorithm header
This program sorts a vector of three int values with std::ranges::sort, but includes only <iostream> and <vector>, compiled as C++20:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 2};
std::ranges::sort(v);
std::cout << v[0] << "\n";
return 0;
}
a_noinclude.cpp: In function ‘int main()’:
$ g++ -std=c++20 a_noinclude.cpp -o a_noinclude_prog
a_noinclude.cpp: In function ‘int main()’:
a_noinclude.cpp:5:18: error: ‘sort’ is not a member of ‘std::ranges’
5 | std::ranges::sort(v);
| ^~~~
Read the message literally: it does not say std::ranges is missing. It says sort is not a member of it. The namespace has been declared - <vector> and <iostream> pull in enough of the library for the compiler to know std::ranges - but no header in the program declares the algorithm itself, so the member lookup fails.
The fix that is not: adding <ranges>
The first instinct is to include the header named after the library. It changes nothing:
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 2};
std::ranges::sort(v);
for (int x : v) std::cout << x << "\n";
return 0;
}
a_fixed.cpp: In function ‘int main()’:
$ g++ -std=c++20 a_fixed.cpp -o a_fixed_prog
a_fixed.cpp: In function ‘int main()’:
a_fixed.cpp:6:18: error: ‘sort’ is not a member of ‘std::ranges’
6 | std::ranges::sort(v);
| ^~~~
The error is identical, word for word. <ranges> declares the views, the range concepts, and the adaptor machinery. It does not declare std::ranges::sort, or any other constrained algorithm. A program can include <ranges>, use every view in it correctly, and still have no std::ranges::sort - the views half of this guide's programs below does exactly that, in reverse.
Neighboring headers: six fail, one works
Swapping in another nearby header as the only extra header, in C++20 mode, mostly changes nothing. Each of these fails with the same first error, 'sort' is not a member of 'std::ranges':
<vector><iterator><numeric><utility><string><ranges>itself, as the section above shows
One of them adds a misleading suggestion: with <functional>, GCC appends did you mean 'std::sort'? and points at the iterator-pair std::sort declared through that header. That suggestion names a different function - the classic two-iterator std::sort - not the ranges overload the program called.
The one header that works is <algorithm>: with <algorithm> as the only extra header, the same program compiles and runs. That is the whole diagnosis - the ranges algorithms are <algorithm>'s contents, exactly as the iterator-pair algorithms are.
The fix
Include <algorithm>, and keep the build in C++20 mode or later:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 2};
std::ranges::sort(v);
for (int x : v) std::cout << x << "\n";
return 0;
}
COMPILE_OK
$ ./g_fixed_prog
1
2
3
The output is the sorted vector, 1, 2, 3. One include declares std::ranges::sort, because the call takes the whole range as one argument - there are no iterator arguments to pass, which is the visible difference from the classic std::sort(v.begin(), v.end()).
Searching and counting with the same header
Every other constrained algorithm follows sort into the program with the same include. This program finds the first element above 10, counts the even elements, and reads the largest and smallest values:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 12, 7, 20, 3};
auto it = std::ranges::find_if(v, [](int x) { return x > 10; });
std::cout << *it << "\n";
std::cout << std::ranges::count_if(v, [](int x) { return x % 2 == 0; }) << "\n";
std::cout << *std::ranges::max_element(v) << "\n";
std::cout << *std::ranges::min_element(v) << "\n";
return 0;
}
COMPILE_OK
$ ./h_fixed_prog
12
2
20
3
The output is 12 (the first value above 10), 2 (the even values, 12 and 20), 20, and 3. find_if, count_if, max_element, and min_element are all std::ranges members from <algorithm>; none of them needs <ranges>.
Sorting by a projection
A ranges algorithm takes its comparison and its projection as arguments, so sorting records by one field is a single call - no lambda that reaches into each element:
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
#include <vector>
struct Item { std::string name; int price; };
int main() {
std::vector<Item> v = {{"pen", 3}, {"book", 12}, {"mug", 7}};
std::ranges::sort(v, std::greater{}, &Item::price);
for (const auto& it : v) std::cout << it.name << " " << it.price << "\n";
return 0;
}
COMPILE_OK
$ ./i_fixed_prog
book 12
mug 7
pen 3
The output orders the items by price, highest first: book 12, mug 7, pen 3. The third argument, &Item::price, is the projection: the algorithm applies it to each element and compares the results with std::greater{}. The elements themselves are never reordered by hand and no separate key array exists.
The other half: views need <ranges>
The split cuts both ways. This program builds a lazy pipeline - keep the even numbers, multiply each by ten - and it includes <ranges> but not <algorithm>:
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5, 6};
auto pipe = v | std::views::filter([](int x) { return x % 2 == 0; })
| std::views::transform([](int x) { return x * 10; });
for (int x : pipe) std::cout << x << "\n";
return 0;
}
COMPILE_OK
$ ./d_fixed_prog
20
40
60
It compiles and prints 20, 40, 60: the views need no algorithm header. But remove <ranges> and keep <algorithm>, and the same kind of program fails on the views side:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3};
auto pipe = v | std::views::transform([](int x) { return x + 1; });
for (int x : pipe) std::cout << x << "\n";
return 0;
}
t_views.cpp: In function ‘int main()’:
$ g++ -std=c++20 t_views.cpp -o t_views_prog
t_views.cpp: In function ‘int main()’:
t_views.cpp:6:26: error: ‘std::views’ has not been declared
6 | auto pipe = v | std::views::transform([](int x) { return x + 1; });
| ^~~~~
Now the message is 'std::views' has not been declared - the views namespace is not declared at all without its header, unlike std::ranges, which other headers declare without filling it. A program that uses both halves includes both headers, as the next program does.
Both headers together: copying a view
This program copies a generated range of integers into a vector and measures a second generated range:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <ranges>
#include <vector>
int main() {
std::vector<int> out;
std::ranges::copy(std::views::iota(1, 6), std::back_inserter(out));
for (int x : out) std::cout << x << "\n";
std::cout << std::ranges::distance(std::views::iota(10, 15)) << "\n";
return 0;
}
COMPILE_OK
$ ./j_fixed_prog
1
2
3
4
5
5
The output is 1 through 5, then 5 - the distance from 10 up to but not including 15. std::ranges::copy comes from <algorithm>, std::ranges::distance comes from <iterator>, and std::views::iota, the view that generates the sequence, comes from <ranges>. No one of those three headers alone declares all three names, which is why this program includes all three.
Sorting a raw array and a string
Ranges algorithms accept any range, not only containers. This program sorts a built-in array in place, then searches and counts inside a string:
#include <algorithm>
#include <iostream>
#include <string>
int main() {
int a[] = {9, 4, 6};
std::ranges::sort(a);
std::cout << a[0] << a[1] << a[2] << "\n";
std::string s = "hello";
std::cout << (std::ranges::find(s, 'l') != s.end()) << "\n";
std::cout << std::ranges::count(s, 'l') << "\n";
return 0;
}
COMPILE_OK
$ ./k_fixed_prog
469
1
2
The output is 469 (the array, sorted in place), 1 (the letter l is found in hello), and 2 (it occurs twice). No begin or end call appears anywhere: the range is the argument.
Traps
A C++17 build fails before any of this, with both headers included:
#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 2};
std::ranges::sort(v);
std::cout << v[0] << "\n";
return 0;
}
u_cpp17.cpp: In function ‘int main()’:
$ g++ -std=c++17 u_cpp17.cpp -o u_cpp17_prog
u_cpp17.cpp: In function ‘int main()’:
u_cpp17.cpp:7:10: error: ‘std::ranges’ has not been declared
7 | std::ranges::sort(v);
| ^~~~~~
The message is 'std::ranges' has not been declared - not "not a member", but not declared at all. The ranges library does not exist in C++17 mode, so no include fixes that build; the fix is the mode itself, -std=c++20 or later.
Dropping the std:: qualification fails even with both headers:
#include <algorithm>
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 2};
ranges::sort(v);
std::cout << v[0] << "\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:7:5: error: ‘ranges’ has not been declared
7 | ranges::sort(v);
| ^~~~~~
There is no namespace named ranges at the top level; the name is std::ranges, and the views are named std::views - an alias for std::ranges::views - not plain views or plain ranges.
And the classic std::sort is not a fallback spelling for the same call: it takes two iterators, not a range, so std::sort(v) is its own error. Converting a ranges call to the classic form means passing v.begin() and v.end() explicitly - a different function that happens to share a name, which is exactly the function GCC's did you mean 'std::sort'? suggestion points at.
Related guides on this site
- 'sort_heap' Is Not a Member of 'std' Fix
- 'partial_sort' Is Not a Member of 'std' Fix
- 'std::sort' Is Not a Member of 'std' Fix
- 'std::unique' Is Not a Member of 'std' Fix
Sources
- cppreference,
<algorithm>header - what it supports: the header that declares the ranges algorithms, includingstd::ranges::sort. - cppreference,
<ranges>header - what it supports: the header that declares the views and range adaptors,std::viewsamong them. - cppreference,
std::ranges::sort- what it supports: the algorithm is a C++20 constrained algorithm declared in<algorithm>, taking a range, an optional comparison, and an optional projection. - cppreference, ranges library - what it supports: the C++20 ranges library as a whole - algorithms, views, and the headers each part lives in.
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.