Fantastic answer is given already. Just as an additional note:
Starting with C++20, several functions in the algorithm library have now also a constexpr
implementation. This includes std::adjacent_find
. And this also in the "ranges" version.
The solution to your problem is nearly given in the cpp reference here with using std::ranges::adjacent_find
.
The example uses std::ranges::greater
as the predicate which would allow for repeating values, resulting in none-strict ordering.
So, we need to use std::ranges::greater_equal. This will also deduce the parameter types of the function call operator from the arguments automatically, which makes live a little bit easier.
The code could look something like the below:
#include <iostream>
#include <cassert>
#include <algorithm>
#include <iterator>
#include <functional>
namespace rng = std::ranges;
template <typename T, size_t N>
constexpr bool increasingValues(T(&a)[N]) noexcept {
return (rng::end(a) == rng::adjacent_find(a, rng::greater_equal()));
}
constexpr int arr[] = { 10, 20, 20, 40, 50};
static_assert(increasingValues(arr), "Array values are not increasing");
int main() {
std::cout << *(std::end(arr) - 1)<< '\n';
}
With Microsoft Visual Studio, the editor would already show the problem.
And the compiler would generate 2 messages with a clear hint.
Static assertion failed with "Array values are not increasing"
Please see a screen shot below:

.
.
.
By the way, the accepted answer compiles like the below

And with fixing to constexpr
, we can see:
