C++ std::midpoint() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++20

Definition

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
CallWhat 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

ParameterDescription
aRequired. The first value or pointer. Integer results are rounded toward it.
bRequired. The second value or pointer. It must have the same type as a.

Return Value

Return ValueDescription
Value of the same typeThe 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.

C++
#include <iostream>
#include <numeric>

int main() {
    std::cout << "midpoint(10, 20) = " << std::midpoint(10, 20) << '\n';
    std::cout << "midpoint(10, 15) = " << std::midpoint(10, 15) << '\n';
    std::cout << "midpoint(15, 10) = " << std::midpoint(15, 10) << '\n';
    return 0;
}

How It Works

  1. 10 and 20 have an exact middle, 15, so no rounding is needed.
  2. 10 and 15 have the middle 12.5. The first argument is 10, which is smaller, so the result is rounded down to 12.
  3. 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;
}

How It Works

  1. The largest int on most machines is 2147483647. Both low and high are close to that limit.
  2. The sum low + high is far bigger than the limit, so writing (low + high) / 2 would overflow. That is undefined behaviour for int.
  3. 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;
}

How It Works

  1. For double values there is no rounding. The middle of 1.0 and 2.0 is exactly 1.5.
  2. 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.
  3. 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>.

Next: partial_sum()

Write the running (prefix) totals of a range to an output with <numeric>.

partial_sum() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful