7

Why is empty struct in C a constraint violation? Why does this rule get changed in C++?

Are there any historical reasons?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Mohit Khullar
  • 245
  • 2
  • 6

2 Answers2

8

since you don't have inheritance in C you don't need them. If you just want to have a distinguishable pointer type you can use pointers to incomplete types.

struct opaque;

struct opaque* stranger = 0;

should work fine.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • I figured you are a macro expert. Could you please look at http://stackoverflow.com/questions/5355241/generating-function-declaration-using-a-macro-iteration whether there is any improvement possible? Thanks! – Johannes Schaub - litb Mar 19 '11 at 11:37
  • @Johannes, I will be busy this afternoon. Have a look into P99 (through my blog). Or contact me offline tonight. – Jens Gustedt Mar 19 '11 at 13:04
3

My guess is this:

In C, there isn't inheritance, templates, and function overloading - three major reasons we use empty structs in C++ - as a base interface, as a template parameter, as a type to help overload resolution.

Can you think of any real use of an empty struct in C?

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • Empty structs can be used handle, no? [Handles Comparison: empty classes vs. undefined classes vs. void*](http://stackoverflow.com/questions/4525847/handles-comparison-empty-classes-vs-undefined-classes-vs-void) – Nawaz Mar 19 '11 at 09:31
  • @Nawaz: Maybe, maybe, I specifically mentioned this was just a guess :) – Armen Tsirunyan Mar 19 '11 at 09:33
  • @Armen: That means, your guess seems to be wrong. Empty structs in C can be used at many occasions, especially where `void*` is used! – Nawaz Mar 19 '11 at 09:34
  • 1
    @Nawaz: I disagree: The mere fact that an empty struct CAN be used as a handle doesn't mean it has to. void*, int, etc. are perfectly good handles too. And using empty structs vs void* without inheritance and overloading isn't really any more advantageous. – Armen Tsirunyan Mar 19 '11 at 09:36
  • 1
    @Armen: Handles of type `void*` can be mixed. You can pass handle to pen to a function which accept brush and the compiler would not detect there is a problem. But if you use empty structs, that would give compile time error. Means, handles of `empty_pen*` and `empty_brush*` cannot be mixed. The latter is type-safe! – Nawaz Mar 19 '11 at 09:38
  • @Nawaz: True, but here's a point: The fact that you found a valid reason why emtpy structs are needed and I found one that they aren't CAN mean that when C was standardized no one thought of yours or thought it wasn't a strong enough reason. Because allowing empty structs complicates things a bit - 0 size vs != 0 size etc... So, I agree with your points, but still think my answer isn't plain WRONG. Of course I may be wrong, in which case let's wait for someone to post a far more convincing answer – Armen Tsirunyan Mar 19 '11 at 09:44
  • @Armen: Standardization complication, or the committee forgot it, is a different thing altogether. All I'm saying is that empty struct has its uses. After all, GDI++ has used it extensively. Even win32 Handle define a member variable called `unused` in HANDLE to avoid zero size issue. If it were allowed, then win32 wouldn't have to define the `unused` member variable in HANDLE struct! – Nawaz Mar 19 '11 at 09:47
  • 1
    @nawaz: You don't need it for that anyway, because you can just leave the types undefined. `struct pen; struct brush;`. – Puppy Mar 19 '11 at 09:54
  • @Armen: Ohh.. how come I forgot that. after all, the link in the first comment also discuss that. :| – Nawaz Mar 19 '11 at 10:04
  • @ArmenTsirunyan : I know the post is old but could you plz give me links or more explanation about the reasons about empty structs beside the tag dispatching for overloading. – Blood-HaZaRd Mar 14 '20 at 17:41