0

Suppose we have an enum class:

enum class E
{
    constant
};

To refer to the enumerator in E, we can write E::constant, while the following is illegal:

E e;
e.constant;

But consider this:

struct S
{
    enum {constant};
};

Both S::constant and s.constant are legal, wouldn't it be more consistent to allow e.constant for an enum class? Is the inability intentional?

Jamboree
  • 5,139
  • 2
  • 16
  • 36
  • 2
    It wouldn't make much sense because `e` is an instance of an `enum`. As such, it can only hold one value. `constant` is not a *member* of the enumeration. It is one of the possible values it can hold. – juanchopanza Oct 28 '14 at 07:23
  • the variable `e` doesn't have to hold the values, the values are something like static member constants. – Jamboree Oct 28 '14 at 07:27
  • `e` can only hold one value. The values are nothing like static member constants. That is the point. – juanchopanza Oct 28 '14 at 07:29
  • Can you find a case where `e.constant` is needed. I.e. not covered by `E::constant`? – RedX Oct 28 '14 at 07:30
  • @RedX in the case that E is deep in some namespace/class hierarchy, e.g. `Ns1::Ns2::C1::C2::E`, then you have to write `e == Ns1::Ns2::C1::C2::E::constant`, so I think it's nice to have `e == e.constant`. – Jamboree Oct 28 '14 at 07:32
  • 2
    I think that `decltype(e)::constant` would solve that problem. – RedX Oct 28 '14 at 07:34
  • @RedX that's true but still not as simple as `e.constant`. – Jamboree Oct 28 '14 at 07:35
  • But `e.constant` makes no sense, so would be confusing to allow. – juanchopanza Oct 28 '14 at 07:39
  • 1
    @juanchopanza that argument also applies to static data members, like `s.constant` I shown in the example, but since C++ allows this syntax anyway, I think it'd be better to make it consistent. – Jamboree Oct 28 '14 at 07:42
  • 3
    The key word being *member*. An enumeration's value is not a member. It is one of the set allowed values an instance of the enumeration can hold. – juanchopanza Oct 28 '14 at 07:45

0 Answers0