Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedef and enum or enum class

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;
like image 683
FreelanceConsultant Avatar asked Nov 13 '22 01:11

FreelanceConsultant


1 Answers

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;
like image 51
Casey Avatar answered Nov 15 '22 05:11

Casey