5

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;
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225

2 Answers2

1

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;
Zak
  • 12,213
  • 21
  • 59
  • 105
Casey
  • 41,449
  • 7
  • 95
  • 125
  • Okay I will look into this one sec I need to fix my code while I remember – FreelanceConsultant Jul 24 '13 at 01:46
  • What does static contexpr do? Actually, what does contexpr do? – FreelanceConsultant Jul 24 '13 at 01:48
  • `constepxr`applied to a variable makes it `const` and forces the compiler to compute its initializer at compile time. In this specific case, it's basically identical to `const` - I'm probably overusing the flashy new construct in places where it works but isn't really necessary. – Casey Jul 24 '13 at 01:51
  • okay I like that, but it is necessary? Once I removed the `class` keyword, it compiled absolutely fine. I can use `my_true` without doing `truth_enum::my_true` - should I be able to do this? – FreelanceConsultant Jul 24 '13 at 02:00
  • Apparently what I described above is the expected behaviours for an enum, but not an enum class. – FreelanceConsultant Jul 24 '13 at 02:22
  • @EdwardBird: The main reason for `enum class` is the namespace hiding *you don't want*. – Ben Jackson Jul 24 '13 at 02:47
  • @EdwardBird `enum class` does two things: (1) restrict the scope of the enumerators, (2) inhibit implicit conversion from enumerator type to `int`. I was assuming that you wanted to keep (2) and workaround (1). If you aren't concerned about (2), then plain `enum` is definitely the solution you need. – Casey Jul 24 '13 at 02:54
  • @Casey Sorry, should have made it clear! Implicit conversion would be good rather than bad actually... – FreelanceConsultant Jul 24 '13 at 10:46
0

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?

FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225