In C++20, std::midpoint() returns the value halfway between two values. It works with integers, floating-point numbers, and pointers.
The old way, (a + b) / 2, can overflow when a and b are big. std::midpoint() is written so that it never overflows, which makes it a safe choice for the middle index of a binary search. For integers it rounds toward the first argument. Include <numeric> and use C++20.
This function is declared in the <numeric> header. It needs C++20 (-std=c++20). It is a free function, called as std::midpoint(...), not with a dot.
Syntax
Give two values of the same type. There are forms for numbers and for pointers.
C++
auto m1 = std::midpoint(a, b); // integers or floating-point
auto m2 = std::midpoint(p1, p2); // pointers into one array
int m = std::midpoint(10, 20); // 15
Call
What it does
midpoint(a, b)
Returns the value halfway between a and b. For integers, the result is rounded toward a.
midpoint(p1, p2)
For pointers into the same array, returns a pointer halfway between them, rounded toward p1.
The order of the arguments matters only for rounding. midpoint(10, 15) is 12, but midpoint(15, 10) is 13, because the result is pulled toward the first argument.
Library declarations
C++
template<class T>
constexpr T midpoint(T a, T b) noexcept; // integers and floating-point
template<class T>
constexpr T* midpoint(T* a, T* b); // pointers into one array
// Added in C++20. Not available for bool.
Parameters
Parameter
Description
a
Required. The first value or pointer. Integer results are rounded toward it.
b
Required. The second value or pointer. It must have the same type as a.
Return Value
Return Value
Description
Value of the same type
The value halfway between a and b. For pointers, a pointer into the same array.
C++ Examples
Example 1 — Midpoint of Two Integers
The midpoint of two integers rounds toward the first argument.
10 and 20 have an exact middle, 15, so no rounding is needed.
10 and 15 have the middle 12.5. The first argument is 10, which is smaller, so the result is rounded down to 12.
When the arguments swap, the first one is 15. The same middle 12.5 is now rounded toward 15, so the result is 13.
Example 2 — Big Numbers Without Overflow
With two very large int values, (a + b) / 2 breaks, but std::midpoint() is safe.
C++
#include <iostream>
#include <numeric>
int main() {
int low = 2147483600;
int high = 2147483646;
// (low + high) / 2 would overflow an int here.
int safe = std::midpoint(low, high);
std::cout << "midpoint = " << safe << '\n';
return 0;
}
Output
midpoint = 2147483623
How It Works
The largest int on most machines is 2147483647. Both low and high are close to that limit.
The sum low + high is far bigger than the limit, so writing (low + high) / 2 would overflow. That is undefined behaviour for int.
std::midpoint() avoids the big sum, and returns 2147483623. This is a safe way to pick the middle index in a binary search.
Example 3 — Midpoint of Doubles and Pointers
Floating-point values give an exact half. Pointers give the element in the middle of an array range.
C++
#include <iostream>
#include <numeric>
int main() {
double d = std::midpoint(1.0, 2.0);
int a[] = {10, 20, 30, 40, 50};
int* mid = std::midpoint(a, a + 4);
std::cout << "double: " << d << '\n';
std::cout << "middle element: " << *mid << '\n';
return 0;
}
Output
double: 1.5
middle element: 30
How It Works
For double values there is no rounding. The middle of 1.0 and 2.0 is exactly 1.5.
The pointer form takes two pointers into the same array. a points to 10 and a + 4 points to 50, which is four steps away.
The pointer halfway is two steps from the start, so it points to 30. Dereferencing it with *mid prints 30.
Frequently Asked Questions
std::midpoint(a, b) returns the value halfway between a and b. It works for integers, floating-point numbers, and pointers into the same array, and it was added in C++20.
Because a + b can overflow when the numbers are large, which is undefined behaviour for signed integers. std::midpoint() never overflows for integers. This matters for the middle index in binary search.
It rounds toward the first argument. midpoint(10, 15) gives 12 and midpoint(15, 10) gives 13. If you need a fractional result, use double values.
Yes. Pass two pointers into the same array and you get a pointer to the element halfway between them, rounded toward the first pointer.
C++20. Compile with -std=c++20 on GCC or Clang, or /std:c++20 on MSVC, and include <numeric>.