相关疑难解决方法(0)

在编译时使用Constexpr填充数组

我想使用constexpr填充一个枚举数组.阵列的内容遵循某种模式.

我有一个枚举将ASCII字符集分为四类.

enum Type {
    Alphabet,
    Number,
    Symbol,
    Other,
};

constexpr Type table[128] = /* blah blah */;
Run Code Online (Sandbox Code Playgroud)

我想有一个128的数组Type.它们可以是一个结构.数组的索引将对应于ASCII字符,值将是Type每个字符的值.

所以我可以查询这个数组,找出ASCII字符属于哪个类别.就像是

char c = RandomFunction();
if (table[c] == Alphabet) 
    DoSomething();
Run Code Online (Sandbox Code Playgroud)

我想知道如果没有一些冗长的宏观黑客,这是否可行.

目前,我通过执行以下操作来初始化表.

constexpr bool IsAlphabet (char c) {
    return ((c >= 0x41 && c <= 0x5A) ||
            (c >= 0x61 && c <= 0x7A));
}

constexpr bool IsNumber (char c) { /* blah blah */ }

constexpr bool IsSymbol (char c) { /* blah blah */ } …
Run Code Online (Sandbox Code Playgroud)

c++ templates constexpr c++11

20
推荐指数
3
解决办法
1万
查看次数

标签 统计

c++ ×1

c++11 ×1

constexpr ×1

templates ×1