假设我们有 const 数组:
const int g_Values[] = { ... };
Run Code Online (Sandbox Code Playgroud)
如何检查成员在编译时单调增长,即g_Values[i] < g_Values[i + 1]
在运行时可以这样检查:
bool IsMonotonously()
{
int i = _countof(g_Values);
int m = MAXINT;
do
{
int v = g_Values[--i];
if (v >= m) return false;
m = v;
} while (i);
return true;
}
Run Code Online (Sandbox Code Playgroud)
但如何用 constexpr 重写它并 if IsMonotonously()return false- 生成编译时错误。