0

For example :

namespace MYGAMESTATE {
    enum class GameState {
        GAME
    };
}

How would I forward declare this enum in another file, since it is in a namespace?

Would it be one of these?

extern enum class MYGAMESTATE::GameState;
extern enum class GameState;
enum class GameState;
enum class MYGAMESTATE::GameState;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
expl0it3r
  • 325
  • 1
  • 7
  • Does this answer your question? [How to forward declare a class which is in a namespace](https://stackoverflow.com/questions/19001700/how-to-forward-declare-a-class-which-is-in-a-namespace) – kmdreko Jul 24 '20 at 21:04
  • 1
    Why do want to forward-declare it in the first place? What are you trying to achieve by that? (It doesn't work for a reason, and while it can be made to work, I doubt you actually need it) – SergeyA Jul 24 '20 at 21:06
  • 2
    Since you are not providing a *storage type*, you cannot forward declare the enum. If it had a storage type, you could do `namespace MYGAMESTATE { enum class GameState : int; }` – Eljay Jul 24 '20 at 21:06
  • 2
    You can't. C++ just forbids this because it's pointless. Just include the header file with the declaration of the enum where you need it. – Zefick Jul 24 '20 at 21:07
  • What do you mean storage type @Eljay – expl0it3r Jul 24 '20 at 23:42
  • @SergeyA I'd like to hide as much code as possible in my header (PImpl) – expl0it3r Jul 24 '20 at 23:43
  • @kmdreko Thank you so much – expl0it3r Jul 24 '20 at 23:43
  • *q.v.* [enumeration declaration](https://en.cppreference.com/w/cpp/language/enum) – Eljay Jul 24 '20 at 23:51

1 Answers1

0

(Mostly compiling an answer from the comments)

If you have to use the enum as in your example, without storage type, you can not do that.

But if you are free to add the storage type, then you can:

Enum (using uint8_t as storage type):

namespace MYGAMESTATE {
    enum class GameState : uint8_t {
        GAME
    };
}

Forward declaration:

namespace MYGAMESTATE { enum class GameState : uint8_t; }
AnnTea
  • 532
  • 7
  • 15