C++
'std::distance' Is Not a Member of 'std' Fix
std::distance lives in one header, <iterator> - a header almost nobody includes on purpose, because in the sweep probed here, covering all 106 standard headers in this library's header set, 44 other headers pull it in on the side, among them <vector>, <iostream>, and <algorithm>. That is why this error is a lean-file error: the textbook combination of <iostream> and <vector> compiles and prints its answer without <iterator> anywhere, and the failure waits for the small file - the header-only helper, the stripped-down test - whose includes happen to pull nothing in. When it does fail, this library version's diagnostic is unusually direct: the plain error line, then one note naming <iterator> and a fix-it snippet that writes the missing include into the file. This guide pins down the failing shape, the 44 transitive rescues, the semantics the fixed call gives back, and the one include that fixes it by rule.
Written December 2, 2026 · Tested with GCC 13.3.0 · 24 min read
- The error: the include is missing, in a lean file
- The textbook version does not fail - it compiles by accident
- Neighboring headers: 61 fail with the same note, and 44 rescue it by accident
- The fix
Written December 2, 2026
Tested with GCC 13.3.0
What this guide is
std::distance is a function template defined in the header <iterator>. It returns the number of increments needed to go from the first iterator to the last - the number of hops between them. The iterators need only meet the input-iterator requirements; for iterators that also meet the random-access requirements the work is constant-time instead of linear, and since C++11 the result may be negative when the first iterator is reachable from the last. The function predates C++11 and has been usable in constant expressions since C++17.
Because the function lives in a header that few programs name directly - <iterator>, not a container's header and not <algorithm> - 'distance' is not a member of 'std' has one cause, and naming it is the first step:
- The
#include <iterator>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 does most of the work: in the sweep below, 44 of the other 105 headers in the library's header set pull the name in on the side, so most files with ordinary includes do not reach this error at all.
There is no version form of this failure. std::distance is an original name, older than C++11, so a file that includes <iterator> and is compiled as C++98 counts correctly, as the versions section below shows. The one layered piece with a floor is constant-expression use, which needs C++17 - a reader who raises the standard flag to chase the plain error is fixing a flag that was not the problem.
A counting program's file most often already holds <vector> for the container and <iostream> for printing, and in the sweep probed here both rescue the name - so the file a reader actually has usually compiles, and the error is left for lean files: a header-only helper, a minimal test, a translation unit whose includes are all C headers or utility headers. 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 outcome splits two ways - the failure with its home-naming note, or a rescue - and the rescue set has 45 entries: <iterator> itself and 44 transitive rescues.
The error: the include is missing, in a lean file
This program counts the elements of a five-element array, but includes only <limits>:
#include <limits>
int main() {
int a[5] = {1, 2, 3, 4, 5};
auto d = std::distance(a, a + 5);
return d == 5 ? 0 : 1;
}
-std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:19: error: 'distance' is not a member of 'std'
4 | auto d = std::distance(a, a + 5);
| ^~~~~~~~
a_limits.cpp:2:1: note: 'std::distance' is defined in header '<iterator>'; did you forget to '#include <iterator>'?
1 | #include <limits>
+++ |+#include <iterator>
2 | int main() {
Read it in full: one plain error line - no suggestion tail rides on it, and there are no help lines - and then exactly one note, and the note does something the neighboring names' diagnostics in this family do not: it names the home header outright and offers a fix-it snippet that inserts the missing include above the file's first line. The same program with no include at all fails the same way, with the same note:
-std=c++17 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:19: error: 'distance' is not a member of 'std'
3 | auto d = std::distance(a, a + 5);
| ^~~~~~~~
n_none.cpp:1:1: note: 'std::distance' is defined in header '<iterator>'; did you forget to '#include <iterator>'?
+++ |+#include <iterator>
1 | int main() {
The textbook version does not fail - it compiles by accident
The version most often written first - print the distance between a vector's ends, in both directions - does not produce this error at all, without <iterator>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4};
std::cout << "distance(first, last) = " << std::distance(v.begin(), v.end()) << "\n";
std::cout << "distance(last, first) = " << std::distance(v.end(), v.begin()) << "\n";
return 0;
}
-std=c++17 t_textbook.cpp -o t_textbook_prog
$ ./t_textbook_prog
distance(first, last) = 3
distance(last, first) = -3
It compiles, runs, and prints both answers - 3 forward and -3 backward - because <vector> and <iostream> each pull <iterator>'s declarations in on the side in this library version. That compile is an accident of this library's internal includes, not a rule of the language: in the sweep probed here the rescue set is exactly the 45 headers listed below, and a leaner file whose includes are outside that set gets the error in the previous section. A program that uses std::distance includes <iterator>, whether or not it happens to compile without it. A sole <vector> file makes the same point in miniature - the call compiles and runs with no <iterator> in sight:
-std=c++17 v_vector.cpp -o v_vector_prog
$ ./v_vector_prog
Neighboring headers: 61 fail with the same note, and 44 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 counts a five-element array and checks the result against 5, the outcome splits by compile result and diagnostic shape two ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 61 headers all fail with the plain is not a member of 'std' error line, no suggestion tail on that line, exactly one note line - the note naming <iterator> quoted in full in the lean-file section above - and no help lines:
<any><atomic><barrier><bit><cassert><cctype><cerrno><cfenv><cfloat><charconv><chrono><cinttypes><ciso646><climits><clocale><compare><concepts><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctime><cuchar><cwchar><cwctype><exception><expected><format><initializer_list><iosfwd><latch><limits><mutex><new><numbers><numeric><ranges><ratio><semaphore><shared_mutex><source_location><span><spanstream><stacktrace><stdfloat><stop_token><syncstream><thread><tuple><type_traits><typeindex><typeinfo><utility><version>
A file with no include at all fails the same way, with the same note. Two entries in that list deserve a pause. <numeric> fails: the header of the other count-shaped names in this family does not declare this one. And <ranges> fails: the C++20 ranges library has its own distance name, in its own namespace, and it does not bring this one in - that name gets its own section below.
And these headers rescue the program - it compiles and runs with no <iterator> anywhere in the file:
<algorithm><array><bitset><ccomplex><cmath><codecvt><complex><condition_variable><ctgmath><deque><execution><filesystem><forward_list><fstream><functional><future><iomanip><ios><iostream><istream><iterator><list><locale><map><memory><memory_resource><optional><ostream><queue><random><regex><scoped_allocator><set><sstream><stack><stdexcept><streambuf><string><string_view><system_error><unordered_map><unordered_set><valarray><variant><vector>
The contrast with the neighboring sweeps is the whole shape of this name. For std::partial_sum the rescue set had three entries; for this name it has 45, because <iterator> sits underneath the container, stream, and algorithm headers - nearly every header that deals in iterators pulls it in. That is also why the failing set is dominated by C headers, utility headers, and concurrency headers: headers with no iterator surface of their own. <iterator> - the home header - is on the rescue list by definition, not by transitivity, and the other 44 rescues are this library version's behavior, not a guarantee.
The fix
Add the header:
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4};
std::cout << "distance(first, last) = " << std::distance(v.begin(), v.end()) << "\n";
std::cout << "distance(last, first) = " << std::distance(v.end(), v.begin()) << "\n";
return 0;
}
-std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
distance(first, last) = 3
distance(last, first) = -3
One include, <iterator>, declares the function - and the output is unchanged, distance(first, last) = 3 and distance(last, first) = -3, because the textbook program was already computing the right answers through the transitive rescue. What the include changes is not the output but the standing of the program: the name is now declared by a header the file names itself, instead of by an internal include of <vector> or <iostream> that a different library version is free to rearrange.
The return value: hops, signs, and subranges
The call returns a count of increments, and its sign and size follow the iterators given to it. Over five-element ranges it reports 5 for both a random-access range and a forward-only one; swapping the ends of a random-access range negates the answer; a pair of reverse iterators counts forward in the reversed direction; and a subrange counts only its own hops:
#include <iostream>
#include <iterator>
#include <list>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
std::list<int> l = {1, 2, 3, 4, 5};
std::cout << "vector " << std::distance(v.begin(), v.end()) << "\n";
std::cout << "list " << std::distance(l.begin(), l.end()) << "\n";
std::cout << "reversed " << std::distance(v.end(), v.begin()) << "\n";
std::cout << "reverse-iterators " << std::distance(v.rbegin(), v.rend()) << "\n";
std::cout << "subrange " << std::distance(v.begin() + 1, v.begin() + 4) << "\n";
return 0;
}
-std=c++17 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
vector 5
list 5
reversed -5
reverse-iterators 5
subrange 3
Read in order: the vector line and the list line both report 5 - the same answer over a random-access range, where the count is computed by subtraction, and over a list, whose bidirectional iterators can only be stepped one at a time. The reversed line reports -5: with random-access iterators, last reachable from first in the other direction gives a negative count, documented since C++11 - and, as the versions section shows, given by this library's build even compiled as C++98. The reverse-iterators line reports 5 again, because a reverse iterator pair describes the same five elements in the other order, counted forward. The subrange line reports 3, the hops from the second element to the fifth - the count is a property of the two iterators passed, not of the container behind them.
The edge ranges: empty and one element
No short range is an error. An empty range - first equal to last - reports 0, and a one-element range reports 1:
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> e;
std::vector<int> one = {8};
std::cout << "empty " << std::distance(e.begin(), e.end()) << "\n";
std::cout << "one " << std::distance(one.begin(), one.end()) << "\n";
return 0;
}
-std=c++17 k_empty.cpp -o k_empty_prog
$ ./k_empty_prog
empty 0
one 1
The empty call reports 0 because there are no increments to make: the first iterator already is the last. The one-element call reports 1 - one hop from the element to the past-the-end iterator. Neither needs a special case at the call site.
Input iterators: the count runs on a stream
The iterator requirement is the weakest one: the iterators need only meet the input-iterator requirements, so the range does not need to be a container at all. This program counts the integers in a stream, read one at a time - a range that cannot be rewound and cannot be indexed:
#include <iostream>
#include <iterator>
#include <sstream>
int main() {
std::istringstream in("1 2 3 4");
std::istream_iterator<int> first(in), last;
std::cout << std::distance(first, last) << "\n";
return 0;
}
-std=c++17 i_stream.cpp -o i_stream_prog
$ ./i_stream_prog
4
It reports 4, the count of the four integers in the stream. The count is made by stepping the first iterator until it reaches the last, which consumes the stream as it goes - the linear form of the call, over the weakest iterators it accepts.
Versions: this name has no second cause - it predates C++11
This call does not need a modern standard. The fixed programs in this guide are compiled as C++17, but the classic call has no version floor at all: with <iterator> included, the same call compiled as C++98 compiles and runs, and compiled as C++11 it reports the same counts - including the negative one:
#include <iostream>
#include <iterator>
int main() {
int a[5] = {1, 2, 3, 4, 5};
std::cout << std::distance(a, a + 5) << " " << std::distance(a + 5, a) << "\n";
return 0;
}
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
5 -5
-std=c++11 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
5 -5
Both standards print 5 -5. The negative result is documented since C++11, and the defect report that clarified it was applied back to C++98 - in this library's build the reversed call gives -5 under either flag, as probed here. So this name's error has exactly one cause with exactly one fix, and the diagnostic cannot separate two causes because there is only one: a missing include fails under any standard - with the home-naming note, in every failing context probed here - and no standard flag rescues the call when the include is missing, while no file with the include present fails for the standard's reason. There is also no execution-policy overload layered on top of the classic call, and that absence splits in two when a reader tries one anyway. Naming the policy with only <iterator> included fails on the policy's name, not the operation's:
#include <iterator>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
auto d = std::distance(std::execution::seq, v.begin(), v.end());
return d == 4 ? 0 : 1;
}
-std=c++17 p_policy_iteratoronly.cpp -o p_policy_iteratoronly_prog
p_policy_iteratoronly.cpp: In function 'int main()':
p_policy_iteratoronly.cpp:5:33: error: 'std::execution' has not been declared
5 | auto d = std::distance(std::execution::seq, v.begin(), v.end());
| ^~~~~~~~~
That wording - has not been declared, naming std::execution - looks like the missing-header shape the policy names in other guides have. Here it is a dead end: add <execution> alongside <iterator>, and the same call still fails, because there is no policy overload of std::distance for the policy to select - the only candidate the compiler lists is the classic two-iterator overload:
#include <execution>
#include <iterator>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4};
auto d = std::distance(std::execution::seq, v.begin(), v.end());
return d == 4 ? 0 : 1;
}
-std=c++17 p_policy_both.cpp -o p_policy_both_prog
p_policy_both.cpp: In function 'int main()':
p_policy_both.cpp:6:27: error: no matching function for call to 'distance(const __pstl::execution::v1::sequenced_policy&, std::vector<int>::iterator, std::vector<int>::iterator)'
6 | auto d = std::distance(std::execution::seq, v.begin(), v.end());
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/iterator:62,
from /usr/include/c++/13/pstl/algorithm_impl.h:13,
from /usr/include/c++/13/pstl/glue_execution_defs.h:50,
from /usr/include/c++/13/execution:34,
from p_policy_both.cpp:1:
/usr/include/c++/13/bits/stl_iterator_base_funcs.h:148:5: note: candidate: 'template<class _InputIterator> constexpr typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator)'
148 | distance(_InputIterator __first, _InputIterator __last)
| ^~~~~~~~
/usr/include/c++/13/bits/stl_iterator_base_funcs.h:148:5: note: template argument deduction/substitution failed:
p_policy_both.cpp:6:27: note: deduced conflicting types for parameter '_InputIterator' ('__pstl::execution::v1::sequenced_policy' and '__gnu_cxx::__normal_iterator<int*, std::vector<int> >')
6 | auto d = std::distance(std::execution::seq, v.begin(), v.end());
| ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The policy forms belong to the algorithm names in <algorithm> and <numeric>; this iterator operation does not have one, at any standard. And since C++17 the classic call is also usable in constant expressions - this program's counts run at compile time, and the assertions on them are part of the build:
#include <array>
#include <iterator>
constexpr bool check() {
std::array<int, 5> a = {1, 2, 3, 4, 5};
return std::distance(a.begin(), a.end()) == 5 && std::distance(a.end(), a.begin()) == -5;
}
static_assert(check());
int main() { return 0; }
-std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
$ ./c_constexpr17_prog
Compiled as C++14, the same program fails - the call is not a constant expression there, and the static assertion cannot hold - so C++17 is the floor for that one layered piece, and for nothing else in this guide.
The neighboring names: advance, next, and prev live in the same header
The three iterator operations this function pairs with do not live in a container's header either. std::advance moves an iterator by a given number of hops, std::next returns an iterator moved forward, and std::prev returns one moved back - and all three are declared by <iterator>, alongside std::distance. They share this name's accident as well: in a file whose only include is <vector>, an std::advance call compiles and runs, and an std::next call compiles and runs - the same transitive rescue through <vector>, probed the same way. They are shown here only as neighbors, not re-taught.
The neighboring names, fixed
The four names together take the same one added header - <iterator> covers all of them, and <vector> covers only the container they walk. The fixed neighbor program reports advance 30, next 20, and prev 40 over 10, 20, 30, 40:
#include <iostream>
#include <iterator>
#include <vector>
int main() {
std::vector<int> v = {10, 20, 30, 40};
auto it = v.begin();
std::advance(it, 2);
std::cout << "advance " << *it << "\n";
std::cout << "next " << *std::next(v.begin()) << "\n";
std::cout << "prev " << *std::prev(v.end()) << "\n";
return 0;
}
-std=c++17 b_neighbours_fixed.cpp -o b_neighbours_fixed_prog
$ ./b_neighbours_fixed_prog
advance 30
next 20
prev 40
Read in order: advancing a copy of the beginning by two hops lands on 30; the next iterator after the beginning points at 20, one hop in; and the iterator before the end points at 40, the last element. One header declares the counting operation and all three moving operations, and a complete iterator program needs it alongside the container's own header.
A different name since C++20: std::ranges::distance
C++20 added a second distance name, and it is a different name - not a new spelling of this one. std::ranges::distance is a function object in the ranges namespace, declared in <ranges>, that also accepts a whole range or an iterator-and-sentinel pair. Compiled as C++20 it reports 3 for the same three-element vector the textbook program counts:
#include <iostream>
#include <ranges>
#include <vector>
int main() {
std::vector<int> v = {3, 1, 4};
std::cout << std::ranges::distance(v) << "\n";
return 0;
}
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
3
Two boundaries keep the names separate. The ranges name needs C++20: compiled as C++17, that same program fails on the namespace itself, with 'std::ranges' has not been declared. And the classic name is not pulled in by the ranges header: in the sweep probed here, a file whose only include is <ranges> fails for std::distance with the same plain error and the same home-naming note as every other failing header. Each name takes its own header and its own standard.
A different wording - and an argument-dependent rescue that depends on the iterators' types
Including <iterator> is not the only half of the name. Over plain arrays, the unqualified call fails:
#include <iterator>
int main() {
int a[5] = {1, 2, 3, 4, 5};
auto d = distance(a, a + 5);
return d == 5 ? 0 : 1;
}
-std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:4:14: error: 'distance' was not declared in this scope; did you mean 'std::distance'?
4 | auto d = distance(a, a + 5);
| ^~~~~~~~
| std::distance
In file included from /usr/include/c++/13/iterator:62,
from g_unqualified.cpp:1:
/usr/include/c++/13/bits/stl_iterator_base_funcs.h:148:5: note: 'std::distance' declared here
148 | distance(_InputIterator __first, _InputIterator __last)
| ^~~~~~~~
That wording - was not declared in this scope, with did you mean 'std::distance'? on the error line and a note pointing at the declaration - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::distance; 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 <iterator>
#include <vector>
int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
auto d = distance(v.begin(), v.end());
return d == 5 ? 0 : 1;
}
-std=c++17 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::distance through them. A list's iterators rescue it the same way - the same unqualified call over a list compiles and runs:
#include <iterator>
#include <list>
int main() {
std::list<int> l = {1, 2, 3, 4, 5};
auto d = distance(l.begin(), l.end());
return d == 5 ? 0 : 1;
}
-std=c++17 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 <array>
#include <iterator>
int main() {
std::array<int, 5> a = {1, 2, 3, 4, 5};
auto d = distance(a.begin(), a.end());
return d == 5 ? 0 : 1;
}
-std=c++17 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:5:14: error: 'distance' was not declared in this scope; did you mean 'std::distance'?
5 | auto d = distance(a.begin(), a.end());
| ^~~~~~~~
| std::distance
In file included from /usr/include/c++/13/bits/stl_algobase.h:66,
from /usr/include/c++/13/array:43,
from g_unqualified_stdarray.cpp:1:
/usr/include/c++/13/bits/stl_iterator_base_funcs.h:148:5: note: 'std::distance' declared here
148 | distance(_InputIterator __first, _InputIterator __last)
| ^~~~~~~~
An initializer list's iterators fail the same way, for the same reason - in this library they are plain pointers too:
#include <initializer_list>
#include <iterator>
int main() {
std::initializer_list<int> il = {1, 2, 3, 4, 5};
auto d = distance(il.begin(), il.end());
return d == 5 ? 0 : 1;
}
-std=c++17 g_unqualified_il.cpp -o g_unqualified_il_prog
g_unqualified_il.cpp: In function 'int main()':
g_unqualified_il.cpp:5:14: error: 'distance' was not declared in this scope; did you mean 'std::distance'?
5 | auto d = distance(il.begin(), il.end());
| ^~~~~~~~
| std::distance
In file included from /usr/include/c++/13/iterator:62,
from g_unqualified_il.cpp:2:
/usr/include/c++/13/bits/stl_iterator_base_funcs.h:148:5: note: 'std::distance' declared here
148 | distance(_InputIterator __first, _InputIterator __last)
| ^~~~~~~~
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. Write the qualifier either way.
The exact rule
std::distanceis declared by#include <iterator>, and by no other header by rule. In the sweep probed here, 61 of the other 105 headers in the library's header set - among them<limits>,<numeric>,<ranges>,<utility>, and<version>- do not declare it, and neither does a file with no include at all; the remaining 44 pull it in transitively, as this library version's behavior, not as a guarantee.'distance' is not a member of 'std'has one cause. The missing-include cause: the<iterator>include is missing and nothing else in the file pulled the name in - in the sweep probed here, every failing sole header fails with one plain error line, no suggestion tail, and no help lines, plus exactly one note line reading'std::distance' is defined in header '<iterator>'; did you forget to '#include <iterator>'?with a fix-it snippet under it, and no probed sole header produces a suggestion class or a declared-but-not-defined link-failure shape for this name. There is no version cause: with the include present, the call compiles and runs as C++98.- In the sweep probed here, 44 headers besides
<iterator>rescuestd::distancetransitively - among them<vector>,<iostream>,<algorithm>,<string>, and<list>- so a file whose only include is one of those compiles and runs the call without<iterator>. That is this library version's behavior, not a guarantee - in the probed set, no header besides<iterator>is required to pullstd::distancein. - The result rules are the documented ones, as the fixed programs here show: the fixed program prints
distance(first, last) = 3anddistance(last, first) = -3for the three-element vector in the two directions; over five elements the semantics program printsvector 5andlist 5,reversed -5for the swapped random-access ends,reverse-iterators 5, andsubrange 3for the three-element middle range. The count is a property of the two iterators passed, not of the container behind them. - The edge ranges are ordinary: an empty range returns 0 - the edge program here prints
empty 0- and a one-element range returns 1 - the edge program printsone 1- because the result is the number of increments from the first iterator to the last, and an empty range needs none. - The classic
std::distancepredates C++11: with<iterator>included it compiles and runs as C++98 and as C++11, reporting5 -5both times for the forward and reversed counts over five elements - the negative result documented since C++11 and applied back to C++98 by defect report - and it is usable in constant expressions since C++17. There is no execution-policy overload of this name at any standard: with only<iterator>included, namingstd::execution::seqfails with'std::execution' has not been declared, and with<execution>included alongside it, the policy call fails withno matching function for call to 'distance'. - The iterators must meet the input-iterator requirements, and the operation is more efficient - constant instead of linear - when they also meet the random-access requirements. The stream program here shows the floor in action: over a stream of integers read one at a time, the count prints
4, made by stepping and consuming the stream. std::advance,std::next, andstd::prevare declared by#include <iterator>as well: in the programs here, an advance call and a next call in a file whose only include is<vector>compile through the same transitive rescue as this guide's name, and the fixed neighbor program, which includes both<iterator>and<vector>, printsadvance 30,next 20, andprev 40. For none of the four names is the container's header the declaring header.std::ranges::distanceis a different name: a function object in namespacestd::ranges, declared in<ranges>, needing C++20 - compiled as C++17 the ranges program here fails with'std::ranges' has not been declared, and compiled as C++20 it prints3for the three-element vector - and in the sweep probed here<ranges>does not declarestd::distance.'distance' was not declared in this scopefor an unqualifieddistanceuse is a different defect: qualify the name asstd::distance- 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 and over an initializer list's iterators it fails again, because in this library those iterators are plain pointers. Qualify the name either way.
The fixes, ranked
- First, check the include list of the file that names
std::distance. If<iterator>is not there, add it - that is the fix, and the diagnostic's own note names it. It holds whether the file failed in a lean context or happened to compile before through a transitive rescue and stopped compiling when its other includes changed. - Second, do not chase the standard flag for this name: with
<iterator>included, the classic call compiles and runs as C++98, so a too-old standard is not a cause of this error - if the include is present and the error persists, the include is in a different file than the one naming the call, or the name is misspelled. The flag matters only for the pieces layered on top: C++17 for constant-expression use, C++20 for the separate ranges name. - Third, do not reach for a container's header because the call walks a container, or for
<algorithm>because the call looks like an algorithm. In the sweep probed here both happen to rescue this name transitively - and that is exactly the accident this guide warns about: the declaring header is<iterator>, and a complete program names it alongside the container's own header. - Fourth, do not remove
<iterator>because a file compiles without it: in the probed set 44 headers rescue it, 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. - Fifth, if the error names
std::executioninstead -'std::execution' has not been declared- do not stop at adding<execution>: there is no execution-policy overload ofstd::distanceto select, so the policy argument itself has to go. The classic call, without a policy, is the form this name has. - Sixth, match the standard to the one piece beyond the classic call that has a floor: constant-expression use needs C++17, and the separate
std::ranges::distancename needs C++20 and its own header,<ranges>. - Seventh, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::distanceas well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array, astd::array, and an initializer list each remove that rescue in the probed set. - Eighth, if the failing name is a neighboring one, the fix is the same header:
advance,next, andprevtake an added<iterator>too - one header for all four iterator operations, and the container's header for the container alone.
Which fix for which cause
#include <iterator>missing,std::distanceused in a file whose only include is one of the 61 headers listed as failing in the sweep section above, or that includes nothing: the message is'distance' is not a member of 'std', with no suggestion tail and no help lines, plus one note line naming<iterator>and a fix-it snippet - add#include <iterator>.std::distanceused in a file whose only include is one of the 44 transitive-rescue headers listed in the sweep section above -<vector>,<iostream>, and<algorithm>among them: no error on this library version - add#include <iterator>anyway, because those rescues are not guaranteed.std::distanceused in a textbook file including<iostream>and<vector>: no error, and the program prints its counts - add#include <iterator>anyway, for the same reason.std::distanceused with<iterator>included, at any standard from C++98 up: no version error - the classic call predates C++11, and the same file reports5 -5compiled as C++98 and as C++11.std::distancecalled with an execution policy, with<iterator>included but<execution>not: the message is'std::execution' has not been declared- and with<execution>added, the message becomesno matching function for call to 'distance', because this name has no policy overload: remove the policy argument and call the classic form.distanceused without thestd::qualifier,<iterator>included, over plain-array arguments, astd::array's iterators, or an initializer list's iterators: the message is'distance' was not declared in this scope; did you mean 'std::distance'?- qualify the name asstd::distance. Over a vector's iterators or a list's iterators, the same unqualified call compiles through argument-dependent lookup - qualify it anyway, so the call does not depend on the iterators' types.
Checklist
- Check the include list of the file that names the call, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Do not check the standard flag for the classic call: this is one of the names where the flag is not a cause - C++98 is old enough for it, and only the layered pieces have floors, C++17 for constant-expression use and C++20 for the separate ranges name. There is no policy overload to match a standard to.
- Do not import the container's header or the algorithm header into this error as the fix:
std::distanceand its three neighbors take<iterator>andstd::vectortakes<vector>, so there are two headers in a complete counting program, one for the iterator operations and one for the container. - Do not assume the textbook includes rescue this name by rule, and do not delete a working
<iterator>line because a file compiles without it. In the sweep here<iostream>and<vector>rescuestd::distanceonly transitively, and the 44 headers that rescue it do so only as this library version's behavior. - Read the return value as a signed count of hops, not a size: it is negative when a random-access range is counted backward, it is 0 for an empty range, and for non-random-access iterators it is made by stepping - which, over a stream, consumes the range being counted. The fixed programs here print
distance(first, last) = 3anddistance(last, first) = -3for the textbook case, andvector 5,list 5,reversed -5,reverse-iterators 5, andsubrange 3for the semantics cases. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
3and-3for the two directions,5and-5under C++98 and C++11,4for the stream count,3for the ranges name at C++20, andadvance 30,next 20, andprev 40for the neighbor calls.
Related guides
- 'std::count' Is Not a Member of 'std' Fix - the other count-shaped question, asked of a different header:
std::counttallies the elements of a range that equal a value, from<algorithm>, wherestd::distancecounts the hops between two iterators, from<iterator>- two counting calls whose home headers are both different from the container's. - 'std::partial_sum' Is Not a Member of 'std' Fix - the opposite rescue shape in the same family:
std::partial_sumcomputes running totals from<numeric>, a header almost nothing pulls in on the side, so its error appears in ordinary files - where this guide's name hides behind 44 transitive rescues and appears only in lean ones.
Sources
- cppreference,
std::distance— forstd::distancebeing a function template defined in<iterator>; for the return being the number of increments needed to go from the first iterator to the last; for the value being allowed to be negative, since C++11, when random-access iterators are used and the first iterator is reachable from the last, with the clarifying defect report LWG 940 applied to C++98; for the iterators needing to meet the input-iterator requirements, and the operation being more efficient - constant instead of linear - when they also meet the random-access requirements; for the function being usable in constant expressions since C++17; and for there being no execution-policy overload among its declarations. - cppreference, Standard library header
<iterator>— for<iterator>being the header whose synopsis declaresstd::advance,std::distance,std::next, andstd::prev, the home-header fact the fix and the neighbor sections of this guide rest on.
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.