Mohit Khullar
Mohit Khullar

Reputation: 245

Empty struct in C vs empty struct in C++

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

Are there any historical reasons?

Upvotes: 7

Views: 2883

Answers (2)

Jens Gustedt
Jens Gustedt

Reputation: 79003

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.

Upvotes: 8

Armen Tsirunyan
Armen Tsirunyan

Reputation: 133122

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?

Upvotes: 3

Related Questions