如何使用枚举作为带有别名的起始参数

How to use enum as starting args with aliases

我正在尝试使用枚举作为起始参数。 它应该作为别名对工作,因此“i”和“info”应该具有相同的值,等等...

我知道可以将 if/else 与标志一起使用,但我想使用例如来完成此操作。用 int 值切换。

#include <iostream>
#include <string>

namespace startFlags {
    enum class flag {
        i, info = 0,
        e, encrypt = 1,
        d, decrypt = 2,
        c, check = 3,
        h, help = 4
    };

    void printFlag(startFlags::flag input) {
        std::cout << "Output: " << input << std::endl; //error
    }
}

有没有其他方法可以处理带有别名的起始参数。

即使基础类型是 int:[=14=,如果您想将它们打印为 int(或其他),您需要转换 enum classes ]

示例:

#include <iostream>

namespace startFlags {
    enum class flag {
        i, info = i,    // both will be 0
        e, encrypt = e, // both will be 1
        d, decrypt = d, // ...
        c, check = c,
        h, help = h
    };

    void printFlag(startFlags::flag input) {

        // Note cast below:

        std::cout << "Output: " << static_cast<int>(input) << '\n';
    }
}

int main() {
    printFlag(startFlags::flag::i);
    printFlag(startFlags::flag::info);
}