Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Use a enum in a namespace, or anonymous namespace


This is an example of the desired use case:

    static obj& some_call (obj& o, enum struct { abandon, save } disposition) { ... };
This is a common case (and should be more common) to avoid using an obscure boolean flag, which can lead to bugs. It shouldn't need a name.

An anonymous namespace just means the name itself won't leak out; under C++ rules I need the name even to specify the enum tag, which is absurd.


Each instance of "enum struct { abandon, save }" would denote a different type, yes? How would you write a compatible definition to go with your prototype?


I don’t care that they are different types; if anything that would be a feature.

The point is to prevent the “mysterious bool arguments” class of error.

The question is if ADL could infer the scope of the enum, as template instant is toon can now infer the right thing and don’t always need the <T> notation.


I agree that you would want them to be different types; otherwise you would just use an unscoped enum. But that implies that if you write…

  /* example.h */
  void f(enum struct { x, y } arg);

  /* example.c */
  void f(enum struct { x, y } arg) {
    /* do something with arg */
  }
…then you've just created a function with two different overloadings based on distinct anonymous types which just happen to be spelled the same way. Without a type name I don't see any way you could define a function whose prototype would be compatible with the forward declaration. You also have conflicting definitions of "x" and "y" with the same names and scope but different types. Perhaps with GNU extensions you could use typeof(x) for the argument and avoid the conflict, but that isn't standard C++.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: