This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

C++ enums and shadowing


Since I don't know what kind of problem this is (bug, improvement
request) or whether I'm just missing something I'm posting this here
as the most generic list first.

Consider this little C++11 program:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
struct foo {
  enum class name { one, two };

  int name;

  void fct(enum name p) { }
};

int main() {
  foo o;
  o.fct(foo::name::one);
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

It will not compile since the compiler complains about the parameter
to the o.fct() call not being a class, namespace, or enum.

Yes, there is the member variable shadowing the enum and this
shouldn't happen in the first place.  But since it happens and g++
gladly accepts it without even a warning there something is amiss.  I
cannot think of a way how I can refer the the values in the enum.  For
old-style enums this isn't a problem but in this case it is.

There are several possibilities:

- the standard is lax and allows this to happen and one cannot access
the enum.  In that case gcc should at least issue a warning

- gcc shouldn't allow the overload

- there is a way to use the enum values (which I'm missing right now).
In that case gcc's error message should be fixed (there is an enum
after all).  To do this the compiler has to search for the enum in
which case it could also emit a helpful message how to circumvent the
problem.


I'm really curious to learn what is right in this case...


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]