Why is empty struct in C a constraint violation? Why does this rule get changed in C++?
Are there any historical reasons?
Why is empty struct in C a constraint violation? Why does this rule get changed in C++?
Are there any historical reasons?
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.
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?
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!struct pen; struct brush;.