Is there automatic name lookup for enumerators in c++?

Viewed 68

As for as I know, currently in c++ there is no support for this.

For example,

class C{
    struct{
        enum {defaulted, opt1, opt2, ...} flag1;
        enum {defaulted, optA, optB, ...} flag2;
    } flags;
    ...
};

Now suppose we have C obj, to use the flag one would do

obj.flags.flag1 = obj.flags.opt1;

which is unnecessarily verbose.

However as the type of obj.flags.flag1 is known, in theory the compiler could lookup the name opt1 in that scope and save some typing.

Because the flags are not used anywhere else, it is preferred not to give names to the types (in fact it is difficult to come up with appropriate names). using enum syntax in c++20 does not solve this, because: it 1) requires the enum types to be named; 2) using multiple enums may lead to name collision, as shown here, since both enums have a defaulted member.

What will be the difficulties to implement this? Has this been proposed to the C++ standard committee and/or implemented in some compiler?

1 Answers

There could easily be an object named opt1 that could be converted to the appropriate enumeration type. It's better for name lookup to behave consistently rather than make special cases based on the types of other operands (partly because types are usually determined after name lookup).

Related