C++
'std::unique' Is Not a Member of 'std' Fix
std::unique lives in one header, <algorithm> - and in the sweep probed here, covering all 106 standard headers in this library's header set, only 5 other headers pull it in on the side, so the textbook <vector> program fails outright. This name's diagnostic is the plainest in its family: all 100 failing sole-header contexts give the one bare error line with no suggestion, no notes, and no help at all - there is not even a wrong name to be tempted by. This guide pins down the failing shapes, why the call returns a new logical end but removes nothing and resizes nothing, why unsorted input keeps its non-consecutive duplicates, and the include that fixes it by rule.
Written December 3, 2026 · Tested with GCC 13.3.0 · 25 min read
- The error: the include is missing
- The textbook version fails too - with the same plain error
- No suggestion in the sweep: not one header offers a wrong name
- Neighboring headers: 100 fail plainly, none offers a suggestion, and 5 rescue it by accident
Written December 3, 2026
Tested with GCC 13.3.0
What this guide is
std::unique is a function template defined in the header <algorithm>. It walks a range once and keeps the first element of every group of consecutive equivalent elements - comparing with operator==, or with a binary predicate the caller supplies - moving each later group's first element forward over the duplicates in front of it. It returns an iterator to the new logical end: the elements in front of that iterator are the kept sequence, with no two neighbors equal, in their original relative order. It does not shorten the underlying sequence, does not erase anything, and does not change any container's size - the elements at and past the returned iterator are still dereferenceable, in a valid but unspecified state. A call is typically followed by the container's erase member function to actually remove that tail. The classic call predates C++11, has an execution-policy overload since C++17, and has been usable in constant expressions since C++20.
Because the function lives in <algorithm> - not in a container's header - 'unique' is not a member of 'std' is the missing include, and naming that cause is the first step:
- 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, that second clause rarely saves anyone: in the sweep below, only 5 of the other 105 headers in the library's header set pull the name in on the side, so most files without<algorithm>do reach this error - and this name has no trap suggestion at all, as the sweep section below shows.
And for this name the missing include is the only cause of the classic call's error. Some names in this family hide a second cause behind the same wording - a standard floor the name starts at - but std::unique predates C++11, so a file that includes <algorithm> compiles this call at every standard level probed here, C++98 included, as the versions section below shows. The pieces layered on top do have floors - the execution-policy overload needs C++17, and constant-expression use needs C++20 - but those floors belong to the added pieces, not to the classic call.
This name's diagnostic is the plainest in the family so far. Of the 100 failing sole-header contexts probed here, every one fails with the plain line alone - no suggestion tail, no note lines, and no help lines, in any of them. No failing context in the sweep produced a different error at all. The sweep covers every one of the 106 standard headers in this library's header set, compiled as the only header in the program, and the rescue set has only 6 entries, because in this library the name is declared by a header that very little of the library includes.
The error: the include is missing
This program compacts the consecutive duplicates in a five-element sorted array, 1, 1, 2, 2, 3, but includes only <limits>:
#include <limits>
int main() {
int a[5] = {1, 1, 2, 2, 3};
auto e = std::unique(a, a + 5);
return 0;
}
-std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:19: error: 'unique' is not a member of 'std'
4 | auto e = std::unique(a, a + 5);
| ^~~~~~
Read it in full: one plain error line - no suggestion tail rides on it, there are no note lines, and there are no help lines. Nothing in the diagnostic names <algorithm> or points anywhere at all. The same program with no include at all fails the same way:
-std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:19: error: 'unique' is not a member of 'std'
3 | auto e = std::unique(a, a + 5);
| ^~~~~~
The textbook version fails too - with the same plain error
The version most often written first - compact the consecutive duplicates in a vector and report how many elements were kept - produces an error in this library, without <algorithm>, and the error is the plain one from the previous section:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 2, 3};
auto e = std::unique(v.begin(), v.end());
std::cout << e - v.begin() << " " << v.size() << "\n";
return 0;
}
-std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:19: error: 'unique' is not a member of 'std'
5 | auto e = std::unique(v.begin(), v.end());
| ^~~~~~
Nothing rescues the call here and nothing distorts it either: <vector> does not pull the algorithm in - in the sweep below it fails for this name, plainly - and <iostream> pulls in no namesake for this name, so the compiler reports the missing name itself. A program that uses std::unique includes <algorithm>, whether or not it happens to compile without it. A sole <algorithm> file makes the point in miniature - the call compiles and runs with nothing else in sight:
-std=c++20 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog
No suggestion in the sweep: not one header offers a wrong name
No header in the sweep decorates this error at all. That sets this name apart from its neighbors in the same family: where std::replace has one header offering an enumerator and std::remove is offered names in several contexts, std::unique is offered nothing, in any sole-header context - not a function, not an enumerator, not a type. There is no suggestion to follow and no suggestion to warn against; the diagnostic is the bare line, 100 times over, and the only move it leaves is the right one: name the declaring header.
That absence is itself the useful fact. A reader who has learned to scan these errors for a did you mean tail will find none here, and should not read the silence as a different kind of problem - it is the same missing include, with a library that declares no name spelled near unique in namespace std itself for the compiler to guess from, in any of the contexts probed here.
Neighboring headers: 100 fail plainly, none offers a suggestion, and 5 rescue it by accident
In a sweep where each of the 106 standard headers in this library's header set was compiled as the only header in a program that compacts the consecutive duplicates in 1, 1, 2, 2, 3 and checks that the returned iterator sits three elements in with 1, 2, 3 in front of it, the outcome splits by compile result two ways - the failure in its one diagnostic shape, or a rescue. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 100 headers all fail with the plain is not a member of 'std' error line - no suggestion tail on that line, no note lines, and no help lines:
<any><array><atomic><barrier><bit><bitset><cassert><ccomplex><cctype><cerrno><cfenv><cfloat><charconv><cinttypes><ciso646><climits><clocale><cmath><codecvt><compare><complex><concepts><condition_variable><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctgmath><ctime><cuchar><cwchar><cwctype><deque><exception><expected><filesystem><format><forward_list><fstream><future><initializer_list><iomanip><ios><iosfwd><iostream><istream><iterator><latch><limits><list><locale><map><memory><memory_resource><mutex><new><numbers><numeric><optional><ostream><queue><random><ranges><ratio><scoped_allocator><semaphore><set><shared_mutex><source_location><span><spanstream><sstream><stack><stacktrace><stdexcept><stdfloat><stop_token><streambuf><string><string_view><syncstream><system_error><thread><tuple><type_traits><typeindex><typeinfo><unordered_map><unordered_set><utility><variant><vector><version>
A file with no include at all fails the plain way. Two entries deserve a pause. <limits> fails plainly: the header of the numeric limits declares nothing close enough to this name to trigger a guess. <ranges> fails plainly for the classic name too: the C++20 ranges library has its own unique name, and it does not bring this one in - that name gets its own section below. And <vector> - the header of the container this call most often works on - fails plainly, as the textbook section above shows. So does <filesystem>, which for its neighbor std::replace is the one header that offers a suggestion: for this name it offers nothing.
No failing header in any shape produced a help line, none produced note lines, and no failing header produced a different error: this name never gets handed to a namesake function, because the standard library has no second function by this name for the call to be measured against.
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><chrono><execution><functional><regex><valarray>
The declaring header in this library sits underneath very little - a time header, a policy header, a function-object header, the regular-expression header, and the numeric-array header - so the rescue set runs to only 5 transitive entries. That is also why the failing set is nearly the whole library here: with 100 of the other 105 headers failing, the missing include shows itself in almost any file that does not name <algorithm>. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other 5 rescues are this library version's behavior, not a guarantee.
The fix
Add the header:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 2, 3};
auto e = std::unique(v.begin(), v.end());
std::cout << "kept";
for (auto it = v.begin(); it != e; ++it) std::cout << " " << *it;
std::cout << " " << v.size() << "\n";
return 0;
}
-std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
kept 1 2 3 5
One include, <algorithm>, declares the function - and the textbook program prints kept 1 2 3 5: the kept sequence 1, 2, 3 in front of the returned iterator, and the size still 5, because the call itself removed nothing. The name is now declared by a header the file names itself, instead of by an internal include of one of the 5 rescuing headers that a different library version is free to rearrange. For a file that does fail - the <limits> shape above, or any other - the same one include is the whole fix.
What the call does: it compacts consecutive duplicates and returns the new logical end
The call returns the new logical end, and that return value is this name's one real subtlety: everything in front of it is the answer, everything from it to the old end is a tail the caller still owns - still dereferenceable, valid but unspecified - and the container's size has not moved. Only consecutive duplicates collapse: a value that appears again after a different value between its appearances is a new group, and its first element is kept:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 2, 3};
auto e = std::unique(v.begin(), v.end());
std::cout << "sorted";
for (auto it = v.begin(); it != e; ++it) std::cout << " " << *it;
std::cout << "\n";
std::cout << "size " << v.size() << " kept " << (e - v.begin()) << "\n";
std::vector<int> u = {1, 2, 1, 1, 3};
auto e2 = std::unique(u.begin(), u.end());
std::cout << "unsorted";
for (auto it = u.begin(); it != e2; ++it) std::cout << " " << *it;
std::cout << "\n";
std::vector<int> w = {4, 4, 4};
auto e3 = std::unique(w.begin(), w.end());
std::cout << "every";
for (auto it = w.begin(); it != e3; ++it) std::cout << " " << *it;
std::cout << "\n";
std::vector<int> mt;
auto e4 = std::unique(mt.begin(), mt.end());
std::cout << "empty " << (e4 == mt.end()) << "\n";
std::vector<int> n = {1, 2, 3};
auto e5 = std::unique(n.begin(), n.end());
std::cout << "nodup";
for (auto it = n.begin(); it != e5; ++it) std::cout << " " << *it;
std::cout << "\n";
std::vector<int> s = {1, 1, 2, 1, 1};
auto e6 = std::unique(s.begin(), s.end());
std::cout << "runs";
for (auto it = s.begin(); it != e6; ++it) std::cout << " " << *it;
std::cout << "\n";
return 0;
}
-std=c++20 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
sorted 1 2 3
size 5 kept 3
unsorted 1 2 1 3
every 4
empty 1
nodup 1 2 3
runs 1 2 1
Read in order: the sorted line reports 1 2 3 - the kept sequence for the sorted input 1, 1, 2, 2, 3, each run of equal neighbors collapsed to its first element. The size line reports size 5 kept 3: the vector is exactly as long as before the call, and 3 is where the returned iterator sits - the call compacted, it did not remove. The unsorted line reports 1 2 1 3 for the input 1, 2, 1, 1, 3: the two consecutive 1s in the middle collapse to one, but the first 1 survives alongside it, because it is not a neighbor of the later run - this call deduplicates runs, not the whole range, and sorting first is what turns it into a whole-range deduplication. The every line reports 4: when every element is equal, the whole range is one group and one element is kept. The empty line reports 1: on an empty range the returned iterator is the end iterator itself. The nodup line reports 1 2 3: a range with no equal neighbors is returned whole. The runs line reports 1 2 1 for 1, 1, 2, 1, 1 - three groups, three kept first elements, the repeated 1 kept twice because a 2 stands between its runs.
Making the size honest is a second, separate step, and it belongs to the container, not to this call:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 2, 3};
auto e = std::unique(v.begin(), v.end());
std::cout << "before " << v.size() << "\n";
v.erase(e, v.end());
std::cout << "after";
for (int n : v) std::cout << " " << n;
std::cout << " " << v.size() << "\n";
return 0;
}
-std=c++20 r_erase.cpp -o r_erase_prog
$ ./r_erase_prog
before 5
after 1 2 3 3
The before line reports 5 - the size right after the unique call, untouched by it. Only the erase call that follows, handed the returned iterator and the end, brings it to after 1 2 3 3: the kept sequence in front, and the size now 3. That two-step shape - algorithm compacts and reports the new end, container erases the tail - is the erase-remove idiom this family of calls is built around.
Forward iterators: the weakest iterators it accepts
The call needs forward iterators - and writable ones, because it moves kept elements forward by assignment. That is one category stronger than a pure reading algorithm needs, and it is why the call cannot run over a stream. A list's iterators qualify, and the call compacts a list the same way it compacts a vector:
#include <algorithm>
#include <iostream>
#include <list>
int main() {
std::list<int> l = {1, 1, 2, 2, 3};
auto e = std::unique(l.begin(), l.end());
std::cout << "list";
for (auto it = l.begin(); it != e; ++it) std::cout << " " << *it;
std::cout << " " << l.size() << "\n";
return 0;
}
-std=c++20 r_list.cpp -o r_list_prog
$ ./r_list_prog
list 1 2 3 5
It reports list 1 2 3 5 - the kept sequence 1, 2, 3 in front of the returned iterator, and the size still 5, with the same erase step owed to the container if the tail is to go. A std::array works the same way - its iterators are random-access, which is more than this call asks for:
-std=c++20 r_array.cpp -o r_array_prog
$ ./r_array_prog
array 1 2 3 5
It reports array 1 2 3 5. The policy overloads ask for forward iterators as well, for the same reason: a parallel execution may need to revisit the range.
Versions: no second cause for the classic call - it predates C++11
This call needs no modern-enough standard, and that is what separates it from some of its neighbors. std::unique predates C++11 - the cppreference page carries no "since" marker on the classic overloads. The fixed programs in this guide are compiled as C++20, but there is no floor below that to hit for the classic call: with <algorithm> included, the same compaction compiled as C++98 compiles and runs:
#include <algorithm>
#include <iostream>
int main() {
int a[5] = {1, 1, 2, 2, 3};
int* e = std::unique(a, a + 5);
std::cout << a[0] << " " << a[1] << " " << a[2] << " " << (e - a) << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
1 2 3 3
And it compiles and runs as C++11, as C++14, and as C++17 the same way, each printing 1 2 3 3 - the kept elements and the kept count. So this name's error has one cause, not two: add <algorithm>, and no standard flag needs to change with it. A file that includes <algorithm> and still sees this error does not have a version problem with the classic call; the include is not reaching the file that names the call, or the name in the file is not this name.
The pieces layered on top of the classic call do carry floors, and naming one splits in two when a reader tries it. This name does have an execution-policy overload, since C++17 - and naming the policy with only <algorithm> included fails on the policy's name, not the algorithm's:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 2, 3};
auto e = std::unique(std::execution::seq, v.begin(), v.end());
return (e == v.begin() + 3) ? 0 : 1;
}
-std=c++20 p_policy_algonly.cpp -o p_policy_algonly_prog
p_policy_algonly.cpp: In function 'int main()':
p_policy_algonly.cpp:5:31: error: 'std::execution' has not been declared
5 | auto e = std::unique(std::execution::seq, v.begin(), v.end());
| ^~~~~~~~~
That wording - has not been declared, naming std::execution - is the missing-header shape, and here adding the header is the whole fix: add <execution> alongside <algorithm>, and the same call compiles and runs, because the policy overload of std::unique exists:
-std=c++20 p_policy_both.cpp -o p_policy_both_prog
$ ./p_policy_both_prog
The policy form belongs to this name, and it carries the C++17 floor. Compiled as C++14, the same two-header program fails on the policy's name again - at that standard the policy names are not there to be declared:
-std=c++14 p_policy_cpp14.cpp -o p_policy_cpp14_prog
p_policy_cpp14.cpp: In function 'int main()':
p_policy_cpp14.cpp:6:31: error: 'std::execution' has not been declared
6 | auto e = std::unique(std::execution::seq, v.begin(), v.end());
| ^~~~~~~~~
And since C++20 the classic call is also usable in constant expressions - this program's compaction happens at compile time, and the assertion on the result is part of the build:
#include <algorithm>
#include <array>
constexpr int check() {
std::array<int, 5> a{1, 1, 2, 2, 3};
auto e = std::unique(a.begin(), a.end());
return (e == a.begin() + 3 && a[0] == 1 && a[1] == 2 && a[2] == 3) ? 1 : 0;
}
static_assert(check());
int main() { return 0; }
-std=c++20 c_constexpr20.cpp -o c_constexpr20_prog
$ ./c_constexpr20_prog
Compiled as C++17, the same program fails - the call is not a constant expression there, and the static assertion cannot hold:
-std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
c_constexpr17.cpp: In function 'constexpr int check()':
c_constexpr17.cpp:5:25: warning: call to non-'constexpr' function '_FIter std::unique(_FIter, _FIter) [with _FIter = int*]' [-Winvalid-constexpr]
5 | auto e = std::unique(a.begin(), a.end());
| ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
from c_constexpr17.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:939:5: note: '_FIter std::unique(_FIter, _FIter) [with _FIter = int*]' declared here
939 | unique(_ForwardIterator __first, _ForwardIterator __last)
| ^~~~~~
c_constexpr17.cpp: At global scope:
c_constexpr17.cpp:8:20: error: non-constant condition for static assertion
8 | static_assert(check());
| ~~~~~^~
c_constexpr17.cpp:8:20: error: 'constexpr int check()' called in a constant expression
c_constexpr17.cpp:3:15: note: 'constexpr int check()' declared here
3 | constexpr int check() {
| ^~~~~
So C++20 is the floor for that layered piece and C++17 is the floor for the policy form - and no standard at any level rescues a missing include, while no include is needed beyond <algorithm> for the classic call at any standard level.
The neighboring names: one side door in the set, and it is not this family
The names a reader most often reaches for alongside this one mostly live in <algorithm> too, and in a file whose only include is <vector> the unique family fails - there is no side door for it in this library, because the header that declares this family is one <vector> does not pull in. std::unique_copy - which writes the compacted sequence into a second range and leaves the source alone - and std::replace fail with the plain error:
-std=c++20 b_uniquecopy_vector.cpp -o b_uniquecopy_vector_prog
b_uniquecopy_vector.cpp: In function 'int main()':
b_uniquecopy_vector.cpp:5:19: error: 'unique_copy' is not a member of 'std'
5 | auto e = std::unique_copy(v.begin(), v.end(), w.begin());
| ^~~~~~~~~~~
-std=c++20 b_replace_vector.cpp -o b_replace_vector_prog
b_replace_vector.cpp: In function 'int main()':
b_replace_vector.cpp:4:10: error: 'replace' is not a member of 'std'
4 | std::replace(v.begin(), v.end(), 1, 9);
| ^~~~~~~
While its other neighbors fail with suggestions: std::remove is offered move, and std::remove_if is offered remove_cv:
-std=c++20 b_remove_vector.cpp -o b_remove_vector_prog
b_remove_vector.cpp: In function 'int main()':
b_remove_vector.cpp:4:19: error: 'remove' is not a member of 'std'; did you mean 'move'?
4 | auto e = std::remove(v.begin(), v.end(), 1);
| ^~~~~~
| move
-std=c++20 b_removeif_vector.cpp -o b_removeif_vector_prog
b_removeif_vector.cpp: In function 'int main()':
b_removeif_vector.cpp:4:19: error: 'remove_if' is not a member of 'std'; did you mean 'remove_cv'?
4 | auto e = std::remove_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });
| ^~~~~~~~~
| remove_cv
And one name in the probed set does not fail at all: std::copy, called in a file whose only include is <vector>, compiles and runs - it is declared by a lower-level header this library's <vector> does pull in, so it has the side door the unique family lacks. That contrast is the lesson of the set: whether a missing <algorithm> shows itself depends on which internal header declares the name, and the only way to know for a given name is to probe it - the unique family, probed here, is outside. The failing neighbors are shown here only as neighbors, not re-taught.
The neighboring names, fixed
Together the five names take one header between them - <algorithm> for all of them, with <vector> covering only the containers they work on. The fixed neighbor program reports all five operations:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 2, 3};
auto e = std::unique(v.begin(), v.end());
std::cout << "unique";
for (auto it = v.begin(); it != e; ++it) std::cout << " " << *it;
std::cout << "\n";
std::vector<int> w = {1, 2, 1, 3, 1};
std::replace(w.begin(), w.end(), 1, 9);
std::cout << "replace";
for (int n : w) std::cout << " " << n;
std::cout << "\n";
std::vector<int> x = {1, 2, 1, 3, 1};
auto e2 = std::remove(x.begin(), x.end(), 1);
std::cout << "remove";
for (auto it = x.begin(); it != e2; ++it) std::cout << " " << *it;
std::cout << "\n";
std::vector<int> y = {1, 2, 3};
std::vector<int> out(3);
std::copy(y.begin(), y.end(), out.begin());
std::cout << "copy";
for (int n : out) std::cout << " " << n;
std::cout << "\n";
std::vector<int> z = {1, 1, 2, 2, 3};
std::vector<int> out2(5);
auto e3 = std::unique_copy(z.begin(), z.end(), out2.begin());
std::cout << "unique_copy";
for (auto it = out2.begin(); it != e3; ++it) std::cout << " " << *it;
std::cout << "\n";
return 0;
}
-std=c++20 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
unique 1 2 3
replace 9 2 9 3 9
remove 2 3
copy 1 2 3
unique_copy 1 2 3
Read in order: unique leaves 1, 2, 3 in front of its new logical end, the consecutive duplicates compacted away; replace has overwritten every 1 in its range with 9, in place; remove leaves 2, 3 in front of the new logical end; copy has written 1 2 3 into its second range; and unique_copy has written 1 2 3 into its second range, the compacted form of its source, which it leaves untouched.
A ranges form since C++20, in the same header
C++20 added a ranges form of this name, and the standard declares it in the same header as the classic name. std::ranges::unique is a function object in the ranges namespace, declared in <algorithm>, that accepts a whole range and returns a subrange - the tail from the new logical end to the range's end, the part a caller hands to erase. Compiled as C++20 it compacts the same vector and reports the same shape of answer:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 2, 3};
auto r = std::ranges::unique(v);
std::cout << "ranges";
for (auto it = v.begin(); it != r.begin(); ++it) std::cout << " " << *it;
std::cout << " " << (r.end() == v.end()) << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges 1 2 3 1
The trailing 1 is the comparison against the vector's end: the returned subrange ends at the past-the-end iterator, exactly as documented, and its beginning is the new logical end the classic form would have returned. Two boundaries and one library behavior keep the forms straight. The ranges form needs C++20: compiled as C++17, that same program fails on the namespace itself, with 'std::ranges' has not been declared:
-std=c++17 r_ranges_cpp17.cpp -o r_ranges_cpp17_prog
r_ranges_cpp17.cpp: In function 'int main()':
r_ranges_cpp17.cpp:6:19: error: 'std::ranges' has not been declared
6 | auto r = std::ranges::unique(v);
| ^~~~~~
And in this library the ranges header does not rescue the ranges form - a file whose only include is <ranges> fails for it with the plain error, and a file whose only include is <array> fails for it the same way, the two transcripts identical in shape:
-std=c++20 r_ranges_rangesonly.cpp -o r_ranges_rangesonly_prog
r_ranges_rangesonly.cpp: In function 'int main()':
r_ranges_rangesonly.cpp:5:27: error: 'unique' is not a member of 'std::ranges'
5 | auto r = std::ranges::unique(a);
| ^~~~~~
-std=c++20 r_ranges_arrayonly.cpp -o r_ranges_arrayonly_prog
r_ranges_arrayonly.cpp: In function 'int main()':
r_ranges_arrayonly.cpp:4:27: error: 'unique' is not a member of 'std::ranges'
4 | auto r = std::ranges::unique(a);
| ^~~~~~
That is this library version's behavior, not the rule: the header the standard names for both forms is <algorithm>, so a complete program names it. The classic name, meanwhile, is not pulled in by the ranges header either: in the sweep probed here, a file whose only include is <ranges> fails for std::unique with the plain error, as the sweep section above lists.
A different wording - and an argument-dependent rescue that depends on the iterators' types
Including <algorithm> is not the only half of the name. Over plain arrays, the unqualified call fails:
#include <algorithm>
int main() {
int a[5] = {1, 1, 2, 2, 3};
auto e = unique(a, a + 5);
return 0;
}
-std=c++20 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:14: error: 'unique' was not declared in this scope
4 | auto e = unique(a, a + 5);
| ^~~~~~
g_unqualified.cpp:4:14: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:234:1: note: 'std::unique'
234 | unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
| ^~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:1153:32: note: 'std::ranges::unique'
1153 | inline constexpr __unique_fn unique{};
| ^~~~~~
That wording - was not declared in this scope, with notes pointing at the declarations of both std::unique and std::ranges::unique - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::unique; adding includes does not bring the name out of namespace std. Swap the arrays for a vector, and the same unqualified call compiles and runs:
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v = {1, 1, 2, 2, 3};
auto e = unique(v.begin(), v.end());
return (e == v.begin() + 3) ? 0 : 1;
}
-std=c++20 g_unqualified_vec.cpp -o g_unqualified_vec_prog
$ ./g_unqualified_vec_prog
That compile is argument-dependent lookup at work, not a rule to rely on: the vector's iterators are std types, so their associated namespace is std, and lookup finds std::unique through them. A list's iterators rescue it the same way - the same unqualified call over a list compiles and runs:
-std=c++20 g_unqualified_list.cpp -o g_unqualified_list_prog
$ ./g_unqualified_list_prog
But the container being a std type is not enough by itself - over a std::array, the same unqualified call fails with the same wording, because in this library a std::array's iterators are plain pointers, and a pointer has no associated namespace:
#include <algorithm>
#include <array>
int main() {
std::array<int, 5> a{1, 1, 2, 2, 3};
auto e = unique(a.begin(), a.end());
return (a[0] == 1) ? 0 : 1;
}
-std=c++20 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:5:14: error: 'unique' was not declared in this scope
5 | auto e = unique(a.begin(), a.end());
| ^~~~~~
g_unqualified_stdarray.cpp:5:14: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
from g_unqualified_stdarray.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:234:1: note: 'std::unique'
234 | unique(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last);
| ^~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:1153:32: note: 'std::ranges::unique'
1153 | inline constexpr __unique_fn unique{};
| ^~~~~~
And with using namespace std; in the file, the qualifier question goes away by a different route - the unqualified call over a vector compiles and runs, because the using-directive brings the name into scope directly:
-std=c++20 g_using_namespace.cpp -o g_using_namespace_prog
$ ./g_using_namespace_prog
The pattern across the probed set is exact: the rescue follows the iterator type, not the container's name - class iterators of std containers carry the name in, pointer iterators do not, whatever container stands behind them - and a using-directive bypasses the question entirely. Write the qualifier either way.
The exact rule
std::uniqueis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, 100 of the other 105 headers in the library's header set - among them<limits>,<ranges>,<vector>,<filesystem>, and<string>- do not declare it, and neither does a file with no include at all; the remaining 5 pull it in transitively, as this library version's behavior, not as a guarantee.'unique' is not a member of 'std'has one cause in the contexts probed here: the<algorithm>include is missing and nothing else in the file pulled the name in - and it comes in one shape only in the sweep probed here: all 100 failing sole headers fail with one error line and no suggestion tail, no note lines, and no help lines. No failing sole header produced a different error, and none offered any name at all.- In the sweep probed here, 5 headers besides
<algorithm>rescuestd::uniquetransitively -<chrono>,<execution>,<functional>,<regex>, and<valarray>- 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, no header besides<algorithm>is required to pullstd::uniquein. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
kept 1 2 3 5for the textbook case; the semantics program printssorted 1 2 3,size 5 kept 3,unsorted 1 2 1 3,every 4,empty 1,nodup 1 2 3, andruns 1 2 1- the call keeps the first element of every group of consecutive equal elements, returns the new logical end, and leaves the range's size and the elements past the returned iterator in place, valid but unspecified. - The erase step follows from the same rule: the erase program prints
before 5andafter 1 2 3 3, becausestd::uniquealone leaves the size at 5, and only the container'serase, handed the returned iterator and the end, makes the size 3. - The classic call's iterators must be forward iterators, and writable: the list program here compacts a list and prints
list 1 2 3 5, and the array program printsarray 1 2 3 5. - The classic
std::uniquepredates C++11: with<algorithm>included it compiles and runs as C++98, reporting1 2 3 3, and as C++11, C++14, and C++17 the same way. The layered pieces have floors: the execution-policy overload starts at C++17 - with only<algorithm>included, namingstd::execution::seqfails with'std::execution' has not been declared; with<execution>included alongside it, the policy call compiles and runs; and compiled as C++14, the same program fails on the policy's name. Constant-expression use starts at C++20: the static-assert program here compiles as C++20 and fails as C++17. - The neighbors split in this library:
std::unique_copyandstd::replaceare declared by#include <algorithm>as well and both fail in a sole<vector>file with the plain error;std::removefails there with the suggestiondid you mean 'move'?andstd::remove_ifwithdid you mean 'remove_cv'?; andstd::copycompiles in that file outright, the one side door in the probed set. The fixed neighbor program printsunique 1 2 3,replace 9 2 9 3 9,remove 2 3,copy 1 2 3, andunique_copy 1 2 3. std::ranges::uniqueis a ranges form of this same name: a function object in namespacestd::ranges, declared in<algorithm>by the standard, needing C++20, returning a subrange whose beginning is the new logical end and whose end is the range's end - compiled as C++17 the ranges program here fails with'std::ranges' has not been declared, and compiled as C++20 it printsranges 1 2 3 1for the five-element vector. In the probes here<ranges>alone does not rescue it in this library - it fails with the plain not-a-member error for the ranges form,<array>alone fails for it the same way, and<ranges>fails plainly for the classic name in the sweep.'unique' was not declared in this scopefor an unqualifieduniqueuse is a different defect: qualify the name asstd::unique- the include alone does not bring the name out of namespacestd. For this name the unqualified call's fate depends on the iterators' types: over plain arrays it fails, as the first unqualified program above shows; over a vector's iterators and over a list's iterators it is rescued through argument-dependent lookup, as the second and third show, because those iterators arestdclass types; over astd::array's iterators it fails again, because in this library those iterators are plain pointers; and withusing namespace std;in the file it compiles outright, as the last unqualified program shows. Qualify the name either way.
The fixes, ranked
- First, check the include list of the file that names
std::unique. If<algorithm>is not there, add it - that is the fix for the only cause of this error. It holds whether the file failed plainly - all 100 of the probed failures did - and whether or not it happened to compile before through one of the 5 transitive rescues and stopped compiling when its other includes changed. - Second, do not wait for a suggestion to confirm the diagnosis: this error offers none, in any probed context. The bare line is the whole diagnostic for this name, and the missing include is its whole cause.
- Third, do not chase a standard flag for the classic call: this name predates C++11, so no flag change fixes this error, and a file with
<algorithm>included that still fails has an include that is not reaching the translation unit, not a version problem. - Fourth, do not reach for a container's header because the call works on containers, or for
<iterator>because the call takes iterators. In the sweep probed here<vector>,<list>,<string>, and<iterator>all fail for this name - none of them is the declaring header by rule. The declaring header is<algorithm>, and a complete program names it alongside the container's own header. - Fifth, do not remove
<algorithm>because a file compiles without it: in the probed set only 5 headers rescue the classic name, and those rescues are not guaranteed - a file that drops the include is one internal-include change away from the error in the first section. - Sixth, do not expect the call to shorten anything: it returns the new logical end and leaves the size where it was, so follow it with the container's
erase- handed the returned iterator and the end - when the tail is actually to go; and do not read the elements past the returned iterator as kept values, because they are valid but unspecified. - Seventh, do not expect unsorted input to come out deduplicated: only consecutive duplicates collapse, so sort the range first when every duplicate is to go - a value that returns after a different value between its appearances is kept, as the unsorted line of the semantics program shows.
- Eighth, if the error names
std::executioninstead -'std::execution' has not been declared- add<execution>alongside<algorithm>and keep the standard at C++17 or later: this name does have a policy overload, and that pair of changes is the whole fix for the policy form. - Ninth, match the standard to the one piece beyond the classic call that has a floor: constant-expression use needs C++20, and the ranges form
std::ranges::uniqueneeds C++20 - with its header still<algorithm>by the standard, not<ranges>. - Tenth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::uniqueas well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array and astd::arrayeach remove that rescue in the probed set.
Related guides on this site
- 'std::unique_ptr' 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, std::unique - the function template is defined in
<algorithm>; it removes all except the first element from every group of consecutive equivalent elements, returns the new logical end, and does not shorten the underlying sequence; the execution-policy overloads are marked since C++17 and the classic overloads constexpr since C++20, with no since marker on the classic calls themselves. - cppreference,
<algorithm>header - the header that declares this name and the other algorithm names used as neighbors here. - cppreference, std::ranges::unique - the C++20 ranges form of this name, a function object in namespace
std::rangesdeclared in<algorithm>, returning a subrange from the new logical end to the range's end.
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.