小编lom*_*omo的帖子

如何检查 const 数组成员在编译时单调增长

假设我们有 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- 生成编译时错误。

c++ constexpr constexpr-function

2
推荐指数
1
解决办法
109
查看次数

标签 统计

c++ ×1

constexpr ×1

constexpr-function ×1