Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 459
Mohit Khullar Avatar asked Mar 19 '11 09:03

Mohit Khullar


Video Answer


2 Answers

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.

like image 193
Jens Gustedt Avatar answered Sep 22 '22 08:09

Jens Gustedt


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?

like image 23
Armen Tsirunyan Avatar answered Sep 19 '22 08:09

Armen Tsirunyan