I have an enum like this: (Actually, it's an enum class)

enum class truth_enum {
    my_true = 1,
    my_false = 0
};

I would like to be able to expose my_true to the global namespace, so that I can do this:

char a_flag = my_true;

Or at least:

char a_flag = (char)my_true;

Instead of this:

char a_flag = truth_enum::my_true;

Is this possible?

I have tried something like this:

typedef truth_enum::my_true _true_;

I receive the error: my_true in enum class truth_enum does not name a type

My guess is that my_true is a value not a type. Is there an alternative which I can do to enable this functionality in my programs?

Not ideal, but I could do something like:

enum class : const char { ... };
const char const_flag_false = truth_enum::my_false;
有帮助吗?

解决方案 2

Solution was easy, the mistake I made was to use enum class instead of enum.

Yeah, so still a bit confused actually - I can now just use the values like:

bool aboolean = (bool)my_true;

Instead of having to do this:

bool aboolean = (bool)truth_enum::my_true;

Why is this?

其他提示

Remove class from the enum definition. I'll assume that you are offended by implicit conversion to int. How about:

static constexpr truth_enum _true_ = truth_enum::my_true;
static constexpr truth_enum _false_ = truth_enum::my_false;

or simply

const truth_enum _true_ = truth_enum::my_true;
const truth_enum _false_ = truth_enum::my_false;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top