C++

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

std::midpoint lives in one header, <numeric> - not in <cmath>, where a math function is expected, and not in <algorithm>. On this library version almost nothing rescues it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, only <execution> and <random> pull the name in on the side, and the textbook combination of <iostream> and <vector> does not. Like its siblings std::gcd and std::lcm, this error offers no note at all - no header is named, no include is suggested - so the fix has to come from knowing the name's home. This guide pins down the one failing shape, the two transitive rescues, and the one include that fixes the error by specification.

Written December 2, 2026 · Tested with GCC 13.3.0 · 25 min read

By the end of this guide
  • The error: the include is missing, in a lean file
  • The textbook version fails too - with no note to follow
  • Neighboring headers: 103 fail the same way, and two rescue it
  • The fix

Written December 2, 2026

Tested with GCC 13.3.0

What this guide is

std::midpoint is a pair of function template overloads defined in the header <numeric>. The first overload computes the midpoint of two values of one arithmetic type: it returns half the sum of the two, and no overflow occurs. If the two arguments have integer type and the sum is odd, the result is rounded towards the first argument; if they have floating-point type, at most one inexact operation occurs. That overload takes part in overload resolution only if the type is an arithmetic type other than bool. The second overload takes two pointers to elements of the same array object and returns a pointer to the element halfway between them - for pointers to elements i and j of an array, a pointer to element i + (j - i) / 2, where the division rounds towards zero; if the two pointers do not point to elements of the same array object, the behavior is undefined. Both overloads have existed since C++20 and are constexpr from their introduction, the arithmetic overload is noexcept, and neither overload throws exceptions.

Because the overloads live in one specific header, 'midpoint' is not a member of 'std' has one common cause:

  • The #include <numeric> 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 suggestion tail on the error line, no help lines - and no note lines at all. That silence is shared with its siblings std::gcd and std::lcm and separates all three from std::invoke, whose failing headers all carry a note naming the home header. The fix has to come from the name's home header, not from the diagnostic.

This name also has a second cause its siblings do not share in practice: std::midpoint is a C++20 name, where std::gcd and std::lcm are C++17 names, so a file that includes <numeric> and is compiled as C++17 gets the identical error - shown in the versions section below. What makes the error worth separating from its relatives is where the name is expected to live. A midpoint function reads like math, so the first header reached for is <cmath>, and the second is <algorithm> - and in the sweep probed here both fail, with the same bare error as every other failing header. 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 only two ways - the one uniform failure, or a rescue - and the rescue set has exactly three entries: <numeric> itself, <execution>, and <random>. Its siblings in this family - std::gcd and std::lcm among them - 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 returns success if the midpoint of 0 and 10 is 5, but includes only <limits>:

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

That is the whole output: one plain error line, and nothing after it. No suggestion tail rides on the error line itself, there are no note lines, and there are no help lines. The same program with no include at all fails the same way, just as bare:

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

The textbook version fails too - with no note to follow

The version most often written first - print the midpoint of two values held in a vector, with std::cout - gets the same error, without <numeric>, and the same silence after it:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {0, 10};
    std::cout << std::midpoint(v[0], v[1]) << "\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:23: error: 'midpoint' is not a member of 'std'
    5 |     std::cout << std::midpoint(v[0], v[1]) << "\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 - and there is no note pointing anywhere. In the sweep probed here, the only headers that rescue std::midpoint besides <numeric> itself are <execution> and <random>, and those rescues are properties of this library version, not rules of the language. A program that uses std::midpoint includes <numeric>, whether or not it happens to compile without it.

Neighboring headers: 103 fail the same way, and two rescue it

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 returns success if the midpoint of 0 and 10 is 5, the outcome splits by compile result two ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 103 headers all fail with the plain is not a member of 'std' error line, no suggestion tail on that line, no note lines at all, and no help lines:

  • <algorithm>
  • <any>
  • <array>
  • <atomic>
  • <barrier>
  • <bit>
  • <bitset>
  • <cassert>
  • <ccomplex>
  • <cctype>
  • <cerrno>
  • <cfenv>
  • <cfloat>
  • <charconv>
  • <chrono>
  • <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>
  • <functional>
  • <future>
  • <initializer_list>
  • <iomanip>
  • <ios>
  • <iosfwd>
  • <iostream>
  • <istream>
  • <iterator>
  • <latch>
  • <limits>
  • <list>
  • <locale>
  • <map>
  • <memory>
  • <memory_resource>
  • <mutex>
  • <new>
  • <numbers>
  • <optional>
  • <ostream>
  • <queue>
  • <ranges>
  • <ratio>
  • <regex>
  • <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>
  • <valarray>
  • <variant>
  • <vector>
  • <version>

A file with no include at all fails the same way, with the same absence of notes. No failing sole header prints a suggestion tail, a note line, or a help line in this sweep.

A sole <cmath> file shows the failing shape in full - and <cmath> is the header this call is most often expected in, since the name is a math function. It is not there. Neither is it in <algorithm>:

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

And these headers rescue the program - it compiles and runs with no <numeric> anywhere in the file:

  • <execution>
  • <numeric>
  • <random>

Two contrasts with the neighboring sweeps are worth holding on to. First, there is no note class, no suggestion class, and no link-failure shape in this sweep: every failing sole header fails at compile time with the same bare error, so the header-naming notes that std::invoke carries - and the declared-but-not-defined link failure that std::swap produces behind five headers - have no counterpart for this name in the probed set. Second, the rescue set is exactly the one std::gcd, std::lcm, and std::reduce - the other <numeric> names probed in this family - produced: the same two transitive rescues, <execution> and <random>. <numeric> - the home header - is on the rescue list by definition, not by transitivity, and the other two rescues are this library version's behavior, not a guarantee.

The fix

Add the header:

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

One include, <numeric>, declares the overloads - and the three calls already show the integer rounding rule. The midpoint of 0 and 10 is 5 exactly. The midpoint of 0 and 11 is also 5, not 6: the sum is odd, and the result rounds toward the first argument. Reversing the pair changes the answer - the midpoint of 11 and 0 is 6 - so for odd sums std::midpoint is not symmetric in its arguments, and the argument order is part of the call's meaning.

The fixed programs: the sum cannot overflow, and rounding follows the first argument

The point of the function is that the midpoint is computed without ever forming a sum that overflows. The boundary program averages values at the edges of int:

C++
#include <climits>
#include <iostream>
#include <numeric>
int main() {
    std::cout << std::midpoint(INT_MAX - 2, INT_MAX) << " "
              << std::midpoint(INT_MIN, INT_MAX) << " "
              << std::midpoint(-9, 10) << " "
              << std::midpoint(-10, -9) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_ints.cpp -o k_ints_prog
$ ./k_ints_prog
2147483646 -1 0 -10

Read in order: the midpoint of INT_MAX - 2 and INT_MAX is 2147483646 - one below the maximum - even though the plain sum of the two arguments cannot be formed in an int at all. The midpoint of INT_MIN and INT_MAX is -1: the true midpoint is -0.5, and the odd sum rounds toward the first argument, INT_MIN. The midpoint of -9 and 10 is 0, the true 0.5 rounded toward -9; and the midpoint of -10 and -9 is -10, the true -9.5 rounded toward -10. In every odd case the answer leans toward the first argument, never toward zero and never away from it as a fixed rule - toward a.

The fixed programs: floating-point values and pointers

For floating-point arguments the same no-overflow rule holds at the top of the range, and the special values pass through the computation:

C++
#include <cmath>
#include <iostream>
#include <limits>
#include <numeric>
int main() {
    double big = std::numeric_limits<double>::max();
    double inf = std::numeric_limits<double>::infinity();
    double nan = std::numeric_limits<double>::quiet_NaN();
    std::cout << std::midpoint(1.0, 3.0) << " "
              << std::midpoint(2.5, 3.5) << " "
              << std::midpoint(big, big) << " "
              << std::midpoint(inf, inf) << " "
              << std::midpoint(nan, 1.0) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_float.cpp -o k_float_prog
$ ./k_float_prog
2 3 1.79769e+308 inf nan

Read in order: the midpoint of 1.0 and 3.0 is 2, and of 2.5 and 3.5 is 3. The midpoint of the largest double with itself is that same value, printed as 1.79769e+308 - the naive (a + b) / 2 would overflow to an infinity here, and this call does not. The midpoint of infinity with itself is inf, and the midpoint of a quiet NaN with 1.0 is nan: the special values are carried through, not trapped.

The second overload takes pointers instead of values. Both pointers must point into the same array object; the result points at the element halfway between them, with the index division rounding towards zero:

C++
#include <iostream>
#include <numeric>
int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    int* mid = std::midpoint(arr, arr + 5);
    int* mid2 = std::midpoint(arr + 4, arr);
    std::cout << *mid << " " << (mid - arr) << " "
              << *mid2 << " " << (mid2 - arr) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 k_ptr.cpp -o k_ptr_prog
$ ./k_ptr_prog
30 2 30 2

The midpoint of the pointers to element 0 and one past element 4 is the pointer to element 2, whose value is 30 - and the second call, element 4 down to element 0, lands on element 2 as well: the index difference is divided rounding towards zero, so the backward walk covers the same middle element here. The two calls are not reverses of each other - the true reverse of the first call, std::midpoint(arr + 5, arr), lands on element 3, because (0 - 5) / 2 rounds towards zero to -2. The two pointers must belong to the same array object: pointers into different arrays are undefined behavior, not a longer distance.

Versions: C++20 to call it, constexpr from the start, and no note either way

The call itself needs C++20, and std::midpoint is constexpr from its introduction, so a static_assert over the call already compiles under C++20 - including the two rounding cases, which are constant-evaluated with the same toward-the-first-argument rule:

C++
#include <numeric>
int main() {
    static_assert(std::midpoint(0, 10) == 5);
    static_assert(std::midpoint(0, 11) == 5);
    static_assert(std::midpoint(11, 0) == 6);
    return 0;
}
Command
$ g++ -std=c++20 c_constexpr20.cpp -o c_constexpr20_prog
$ ./c_constexpr20_prog

Under C++17 the name is not there at all, even with <numeric> included - and here this error's silence costs the most, because the output is identical to the missing-include case: the same plain error, and no version note to separate a too-old standard from a missing header:

Terminal
$ g++ -std=c++17 c_mid17.cpp -o c_mid17_prog
c_mid17.cpp: In function 'int main()':
c_mid17.cpp:3:17: error: 'midpoint' is not a member of 'std'
    3 |     return std::midpoint(0, 10) == 5 ? 0 : 1;
      |                 ^~~~~~~~

Its sibling std::invoke gets a note naming the standard in exactly this situation; std::midpoint does not. The version split inside this family runs the other way from the include question: std::gcd and std::lcm are C++17 names, and std::midpoint is the newer one - so for this name the too-old-standard cause is the live one whenever a project still builds as C++17. When the include is already in the file and the error persists, the standard flag is the next thing to check - the diagnostic will not tell you.

The neighboring names: gcd and lcm share the one header

The names std::midpoint is used alongside live in the same header. std::gcd - the greatest-common-divisor function, a C++17 constexpr template over two integer types - fails in the lean context here, only <limits> included, with its own plain error and the same absence of a note. It is shown here only as a neighbor, not re-taught:

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

std::lcm - the least-common-multiple counterpart, also C++17 and constexpr - fails the same way in the same lean context:

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

The neighboring names, fixed

Each neighboring name works under the same one added header - there is no second header to reach for in this family. The fixed neighbor program reports 6 36 5: std::gcd(12, 18) is 6, std::lcm(12, 18) is 36, and std::midpoint(0, 10) is 5:

C++
#include <iostream>
#include <numeric>
int main() {
    std::cout << std::gcd(12, 18) << " "
              << std::lcm(12, 18) << " "
              << std::midpoint(0, 10) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 b_neighbours_fixed.cpp -o b_neighbours_fixed_prog
$ ./b_neighbours_fixed_prog
6 36 5

One header, <numeric>, covers the whole conversation: gcd, lcm, and midpoint all take it, and a file that uses any of them and lacks it gets the same plain error with the name swapped. One version split does run through the family: std::gcd and std::lcm are C++17 names, while std::midpoint is newer - in the programs probed here it is absent under C++17 even with <numeric> included, and present under C++20.

A different wording - and no argument-dependent rescue for this name

Including <numeric> is not the only half of the name. This program includes the header, drops the std:: qualifier, and passes plain integers:

C++
#include <numeric>
int main() {
    return midpoint(0, 10) == 5 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:3:12: error: 'midpoint' was not declared in this scope; did you mean 'std::midpoint'?
    3 |     return midpoint(0, 10) == 5 ? 0 : 1;
      |            ^~~~~~~~
      |            std::midpoint
In file included from g_unqualified.cpp:1:
/usr/include/c++/13/numeric:249:5: note: 'std::midpoint' declared here
  249 |     midpoint(_Tp* __a, _Tp* __b) noexcept
      |     ^~~~~~~~

That wording - was not declared in this scope, with did you mean 'std::midpoint'? on the error line and a note pointing at a declaration - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::midpoint; adding includes does not bring the name out of namespace std. One detail in the note is worth seeing for what it is: the declaration it points at, line 249 of <numeric>, is the pointer overload - the compiler names the first declaration it has for the name, not the overload the call wanted.

For std::reduce, the same unqualified call compiles when its arguments are std iterators, because argument-dependent lookup finds the name from the arguments. For std::midpoint there is no such case in the programs probed here: integers are fundamental types and have no associated namespace - and neither do pointers, which are fundamental types too, so the unqualified pointer call fails with the same wording and the same note:

C++
#include <numeric>
int main() {
    int arr[5] = {10, 20, 30, 40, 50};
    return *midpoint(arr, arr + 5) == 30 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 g_unqualified_ptr.cpp -o g_unqualified_ptr_prog
g_unqualified_ptr.cpp: In function 'int main()':
g_unqualified_ptr.cpp:4:13: error: 'midpoint' was not declared in this scope; did you mean 'std::midpoint'?
    4 |     return *midpoint(arr, arr + 5) == 30 ? 0 : 1;
      |             ^~~~~~~~
      |             std::midpoint
In file included from g_unqualified_ptr.cpp:1:
/usr/include/c++/13/numeric:249:5: note: 'std::midpoint' declared here
  249 |     midpoint(_Tp* __a, _Tp* __b) noexcept
      |     ^~~~~~~~

So for this name the qualifier rule has no exception to learn: an unqualified midpoint call is always the qualifier defect, never a working shortcut - over values and over pointers alike.

One type for both arguments: the error that comes from the candidate list

One more failure shape belongs in this guide because it is so easily mistaken for the missing-name error's family: the header is included, the name is qualified, and the call still fails - because the two arguments do not share one type. The arithmetic overload is a template over a single type, so an int and a double cannot be averaged directly; the failure is a no matching function error followed by the candidate list, with the deduction conflict named in a note:

C++
#include <numeric>
int main() {
    return std::midpoint(0, 10.0) == 5.0 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 g_mixed.cpp -o g_mixed_prog
g_mixed.cpp: In function 'int main()':
g_mixed.cpp:3:25: error: no matching function for call to 'midpoint(int, double)'
    3 |     return std::midpoint(0, 10.0) == 5.0 ? 0 : 1;
      |            ~~~~~~~~~~~~~^~~~~~~~~
In file included from g_mixed.cpp:1:
/usr/include/c++/13/numeric:214:5: note: candidate: 'template<class _Tp> constexpr std::enable_if_t<__and_v<std::is_arithmetic<_Tp>, std::is_same<typename std::remove_cv< <template-parameter-1-1> >::type, _Tp>, std::__not_<std::is_same<_Tp, bool> > >, _Tp> std::midpoint(_Tp, _Tp)'
  214 |     midpoint(_Tp __a, _Tp __b) noexcept
      |     ^~~~~~~~
/usr/include/c++/13/numeric:214:5: note:   template argument deduction/substitution failed:
g_mixed.cpp:3:25: note:   deduced conflicting types for parameter '_Tp' ('int' and 'double')
    3 |     return std::midpoint(0, 10.0) == 5.0 ? 0 : 1;
      |            ~~~~~~~~~~~~~^~~~~~~~~
/usr/include/c++/13/numeric:249:5: note: candidate: 'template<class _Tp> constexpr std::enable_if_t<is_object_v<_Tp>, _Tp*> std::midpoint(_Tp*, _Tp*)'
  249 |     midpoint(_Tp* __a, _Tp* __b) noexcept
      |     ^~~~~~~~
/usr/include/c++/13/numeric:249:5: note:   template argument deduction/substitution failed:
g_mixed.cpp:3:25: note:   mismatched types '_Tp*' and 'int'
    3 |     return std::midpoint(0, 10.0) == 5.0 ? 0 : 1;
      |            ~~~~~~~~~~~~~^~~~~~~~~

A bool call is rejected in the same shape, not by a static assertion: std::midpoint(true, false) fails with no matching function for call to 'midpoint(bool, bool)', because the arithmetic overload takes part in overload resolution only for arithmetic types other than bool, so for bool there is no candidate left to call. That is a different refusal mechanism from std::lcm's, whose non-integer and bool arguments are refused by static assertions from inside the header after the name is found. Neither shape is an include problem: convert both values to one shared arithmetic type - explicitly, so the conversion is visible - rather than adding headers.

The exact rule

  • std::midpoint is declared by #include <numeric>, and by no other header by rule. In the sweep probed here, every one of the other 103 headers in the library's header set - among them <algorithm>, <cmath>, <iostream>, <vector>, <utility>, and <version> - does not declare it, and neither does a file with no include at all.
  • 'midpoint' is not a member of 'std' means the <numeric> include is missing and nothing else in the file pulled the name in - or, under a standard before C++20, that the name does not exist yet in that standard. In every context probed here, GCC prints no suggestion tail on the error line, no note lines, and no help lines for this name - the missing-include case and the too-old-standard case print identically, and only the file's own include list and standard flag separate them.
  • In the sweep probed here, the split is two-way: a sole header either fails with that one uniform bare shape, or rescues the program. No probed sole header produces a note class, a suggestion class, or a declared-but-not-defined link-failure shape for this name - the contrast with std::invoke, whose failing headers all carry a note naming <functional>.
  • In the sweep probed here, only two headers besides <numeric> rescue std::midpoint transitively - <execution> and <random>, the same two that rescue std::gcd, std::lcm, and std::reduce - so a file whose only include is one of those compiles and runs the call without <numeric>. That is this library version's behavior, not a guarantee - in the probed set, neither header is required to pull std::midpoint in.
  • The integer call cannot overflow and rounds toward its first argument: the fixed programs here print 5 5 6 for std::midpoint(0, 10), std::midpoint(0, 11), and std::midpoint(11, 0), and 2147483646 -1 0 -10 for std::midpoint(INT_MAX - 2, INT_MAX), std::midpoint(INT_MIN, INT_MAX), std::midpoint(-9, 10), and std::midpoint(-10, -9) - the midpoint of INT_MAX - 2 and INT_MAX is formed even though their plain sum cannot be, and every odd sum leans toward the first argument.
  • The floating-point call follows the same no-overflow rule: the fixed program prints 2 3 1.79769e+308 inf nan - the midpoint of the largest double with itself is the value itself, not an infinity; the midpoint of infinity with itself is inf; and a quiet NaN in an argument makes the result nan.
  • The pointer overload returns a pointer into the same array: the fixed program prints 30 2 30 2 - std::midpoint(arr, arr + 5) and std::midpoint(arr + 4, arr) over a five-element array both land on element 2, whose value is 30, the index division rounding towards zero. Two pointers into different array objects are undefined behavior.
  • std::midpoint is a C++20 pair of overloads and is constexpr since C++20: a static_assert over the call compiles under C++20, and under C++17 the name itself is absent with the same plain error and no note. The arithmetic overload is noexcept, and neither overload throws exceptions.
  • std::gcd and std::lcm are declared by the same #include <numeric>: in the lean programs here, which include only <limits>, each gets its own is not a member of 'std' error, each with no note - and the fixed neighbor program prints 6 36 5 for std::gcd(12, 18), std::lcm(12, 18), and std::midpoint(0, 10).
  • 'midpoint' was not declared in this scope for an unqualified midpoint use is a different defect: qualify the name as std::midpoint - the include alone does not bring the name out of namespace std. For this name no argument-dependent-lookup case rescues the unqualified call in the programs probed here: integer arguments have no associated namespace, and neither do pointer arguments, so both unqualified programs fail with the same wording.
  • Both arguments share one type, and that type is not bool: with <numeric> included, an int/double mix fails with no matching function for call to 'midpoint(int, double)' and a note reporting deduced conflicting types for the parameter, and a bool call with no matching function for call to 'midpoint(bool, bool)'. Those are argument defects, not include defects.

The fixes, ranked

  • First, check the include list of the file that names std::midpoint. If <numeric> is not there, add it - that is the fix in the common case, and it holds whether the file failed in a lean context, failed in a textbook file, or happened to compile before through a transitive rescue.
  • Second, do not wait for a note to confirm the header - for this error there is none. In the contexts probed here the diagnostic is one bare line; take the fix from the name's home header, <numeric>, not from the output.
  • Third, do not reach for <cmath> because the call is a math function, or for <algorithm> because it reads like an algorithm. In the sweep probed here, both fail for std::midpoint with the same error - the numeric functions of this family live in <numeric>, alongside std::gcd and std::lcm.
  • 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 <numeric> because a file compiles without it: in the probed set only <execution> and <random> rescue it, and those rescues are not guaranteed.
  • Fifth, check the standard before changing the code when the include is already right. Under C++17 this name does not exist; under C++20 it exists and is already constexpr, so a static_assert over the call needs nothing newer. The diagnostic will not separate these cases, so read the build's standard flag directly.
  • Sixth, if the error says was not declared in this scope instead, fix the name's qualifier to std::midpoint as well - and do not look for an argument-dependent-lookup case that makes the unqualified call work, because for integer and pointer arguments there is none.
  • Seventh, if the failure is a no matching function error with a candidate list - a mixed int/double call, or a bool call - the include and the qualifier are both already right: convert the arguments to one shared arithmetic type, explicitly, and never bool.
  • Eighth, if the failing name is a neighboring one, the fix is the same header: gcd and lcm take the same added <numeric> - one include for the whole family.

Which fix for which cause

  • #include <numeric> missing, std::midpoint used in a file whose only include is one of the 103 headers listed as failing in the sweep section above, or that includes nothing: the message is 'midpoint' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add #include <numeric>.
  • std::midpoint used in a textbook file including <iostream> and <vector>: the message is the same bare error - add #include <numeric>.
  • std::midpoint used with only <numeric>, <execution>, or <random> included: no error on this library version - add #include <numeric> anyway when it is not the include already present, because the <execution> and <random> rescues are not guaranteed.
  • std::midpoint used with <numeric> included under C++17: the message is 'midpoint' is not a member of 'std' with no note - compile as C++20 or later. A static_assert over the call needs only C++20, where the overloads are already constexpr.
  • std::gcd or std::lcm used without <numeric> in the lean context: the message names that name, with no note - add #include <numeric>. For those names the standard matters less: both already exist under C++17.
  • midpoint used without the std:: qualifier, <numeric> included, over integer arguments or over pointers into an array: the message is 'midpoint' was not declared in this scope; did you mean 'std::midpoint'? - qualify the name as std::midpoint.
  • std::midpoint called with arguments of two different types, or with bool arguments, <numeric> included: the message is a no matching function error with a candidate list - midpoint(int, double) with a deduced-conflicting-types note, or midpoint(bool, bool) - convert the arguments to one shared arithmetic type; no include change fixes this shape.

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 read the absence of a note as a different error. For this name the missing-include diagnostic is one bare line in every probed context; the note that names the home header for std::invoke has no counterpart here.
  • Do not import a neighboring family's split into this error: in this family every name - gcd, lcm, midpoint - takes the same <numeric>, so there is no per-name header to look up.
  • Do not assume the textbook includes rescue this name, and do not delete a working <numeric> line because a file compiles without it. In the sweep here <iostream> and <vector> do not rescue std::midpoint at all, and the two headers that do rescue it do so only transitively.
  • Match the standard to the use: the call itself needs C++20 and <numeric> - one standard step more than its C++17 siblings std::gcd and std::lcm - and a constant-expression call needs nothing more, since the overloads are constexpr from C++20.
  • Do not assume the call is symmetric: for an odd integer sum the result rounds toward the first argument, so std::midpoint(0, 11) is 5 and std::midpoint(11, 0) is 6. Code that swaps the arguments to sort them changes the answer on odd sums.
  • Use the call where the plain average would overflow: std::midpoint(INT_MAX - 2, INT_MAX) is 2147483646, formed without ever forming the sum - and keep pointer calls inside one array object, since two pointers into different arrays are undefined behavior.
  • Check the argument types before adding headers: a failure whose first error is no matching function with a candidate list is an argument defect - the two arguments must share one arithmetic type, and that type must not be bool.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 5 5 6 for the plain and rounding calls, 2147483646 -1 0 -10 for the boundary integer cases, 30 2 30 2 for the pointer calls, and 6 36 5 for the neighbor calls.
  • 'std::gcd' Is Not a Member of 'std' Fix - the same missing-include shape for the greatest-common-divisor counterpart in <numeric>: its sweep is identical in shape to this one - the same 103 uniform bare failures and the same two transitive rescues, <execution> and <random> - and the two guides differ mainly in the version rule and the argument rule, since std::gcd is a C++17 name whose two arguments may have different integer types, where std::midpoint is a C++20 name whose two arguments share one type.
  • 'std::lcm' Is Not a Member of 'std' Fix - the same missing-include shape for the least-common-multiple counterpart in <numeric>: its sweep is identical in shape to this one - the same 103 uniform bare failures and the same two transitive rescues, <execution> and <random> - and the two guides differ mainly in the refusal mechanism for bad arguments, since std::lcm refuses non-integer and bool arguments with static assertions from inside the header, where std::midpoint refuses mixed-type and bool calls earlier, at overload resolution, with a no matching function error.

Sources

  • cppreference, std::midpoint — for std::midpoint being a pair of function template overloads defined in <numeric> since C++20, both constexpr and the arithmetic overload noexcept, the arithmetic overload returning half the sum of its arguments with no overflow occurring, rounding towards the first argument when the arguments are integers whose sum is odd, performing at most one inexact operation for floating-point arguments, and taking part in overload resolution only when the type is an arithmetic type other than bool; for the pointer overload returning a pointer to element i + (j - i) / 2 of the array both pointers point into, the division rounding towards zero, with undefined behavior when the pointers do not point to elements of the same array object; for neither overload throwing exceptions; and for the feature-test macro __cpp_lib_interpolate (201902L, C++20), shared with std::lerp.
  • cppreference, Standard library header <numeric> — for <numeric> being the header that declares std::midpoint in both overloads alongside std::gcd, std::lcm, std::accumulate, std::reduce, std::inner_product, std::transform_reduce, std::partial_sum, the scan family, and std::adjacent_difference.
  • cppreference, std::gcd — for std::gcd being a function template defined in <numeric> since C++17, constexpr since C++17, whose two arguments may have different integer types and whose result has their common type - the version and argument contrasts the neighbor sections of this guide rest on.
  • cppreference, std::lcm — for std::lcm being a function template defined in <numeric> since C++17, constexpr since C++17, that is ill-formed when an argument type is not an integer type or is bool and refuses those arguments with static assertions in this library - the refusal-mechanism contrast with std::midpoint's no matching function shape.

Keep Exploring