1

I understand that this may be a duplicate question. However, none of the answers to the relevant questions convinced me.

My question is that: why C++ allows forward declaration of class but not enum?

I understand that if you specify the underlying type of the enum or use enum class in C++, you can forward declare enum type. But why does C++ needs to know the size of underlying type?

enum E;
void foo(E e); // Error

class C
void bar(C c); // Correct, Compiler doesn't know the size of class C either.
Oliver Young
  • 578
  • 1
  • 4
  • 12
  • @Resurrection As mentioned in my question, the answers in your link didn't convince me. My question is why does C++ compiler need to know the size of enum to be able to forward declare enum. – Oliver Young Jun 17 '19 at 16:41
  • 2
    Looks like just a quirk of the language to me. – HolyBlackCat Jun 17 '19 at 16:42
  • 1
    Because it needs to generate your function and it needs to know how large each argument is. – Resurrection Jun 17 '19 at 16:42
  • @Resurrection Yet when you forward declare a class you don't know the size of the class. – HolyBlackCat Jun 17 '19 at 16:43
  • @Resurrection If that's the answer, why does C++ allow forward declaration of class? The compiler apparently doesn't need to the size of parameters when declare functions. – Oliver Young Jun 17 '19 at 16:44
  • 1
    ***class C; void bar(C c); // Correct, Compiler doesn't know the size of class C either.*** Is this correct? Doesn't C need to be a reference or pointer? – drescherjm Jun 17 '19 at 16:44
  • @Resurrection A bit poorly formulated but hits the nail. Also this is fairly well explained in the duplicates answers. – πάντα ῥεῖ Jun 17 '19 at 16:44
  • @drescherjm Yes: https://godbolt.org/z/xdKbR_ – Max Langhof Jun 17 '19 at 16:44
  • @drescherjm You could try yourself : ) – Oliver Young Jun 17 '19 at 16:45
  • 1
    @HolyBlackCat That is because `enum` in C++ is not a type or separate entity. It is a syntactic sugar for `int` in majority of the cases. `class` is declarator of the type and is substituted by its concrete definition just like `enum class`. And if you deviate the latter's underlying type in its definition from forward declaration you get a compiler error. – Resurrection Jun 17 '19 at 16:45
  • 1
    The answer is definitely not as simple as "the compiler needs to know the size", or "plain enums are not unique types": https://godbolt.org/z/jgjLzW - I don't see any answer in the linked duplicate that gives a good reason. I still think that means the duplicate needs a better answer, not a different question. – Max Langhof Jun 17 '19 at 16:50
  • 3
    @Resurrection Based on your reasoning, I still don't see a reason why c++ compiler cannot support forward declaration of enum without knowing its underlying size. After all, compiler doesn't need to know the size of function parameter to declare a function. Maybe it's just a quirk of the language. – Oliver Young Jun 17 '19 at 16:51
  • @MaxLanghof Can you explain your point of your example? The reason enum class compiles is because compiler has a default underlying type of enum class (which is int) – Oliver Young Jun 17 '19 at 16:52
  • as comment in the other comment, `typedef` cannot be forward declared neither. – Jarod42 Jun 17 '19 at 16:57
  • The answer cannot be "the compiler doesn't know the size" - your question has the reason for that. The answer _also_ cannot be that `enum` is just syntactic sugar for `int` by default - otherwise you'd get name mangling problems (and more) when trying to e.g. overload functions on different plain enum types - different enum names clearly identify different types in the same way different class names do. I do believe @HolyBlackCat is right about it being a language quirk. – Max Langhof Jun 17 '19 at 16:57
  • @drescherjm not for the prototype, I think. The implementation, yes for sure. Of course, it will need to know that for other reasons too, like calling a public function on class C. –  Jun 17 '19 at 17:00
  • @Resurrection That's between oversimplified and wrong. Different enum names do represent different types, otherwise [this](https://godbolt.org/z/wlmYmV) would be disallowed. In this regard, the compiler has no more information from `class X;` than `enum Y;` and I see no grounds to forbid the latter. – Max Langhof Jun 17 '19 at 17:01
  • Now I'm second guessing myself. You may be right. Unfortunately I'm not in front of compiler right now to test. –  Jun 17 '19 at 17:02
  • @Chipster https://godbolt.org is your friend :) – Max Langhof Jun 17 '19 at 17:03
  • 2
    It was not allowed in C++ back then because it is not allowed in C. It is not allowed in C because there was no pressing need for such things back in 1980. It is not allowed in C++ now because no one cares. People should not be using old-style enums in new development anyway, why revise rules that govern them? – n. m. could be an AI Jun 17 '19 at 17:38

0 Answers0