Empty struct in C vs empty struct in C++

风流意气都作罢 提交于 2021-02-07 02:49:29

问题


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

Are there any historical reasons?


回答1:


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.




回答2:


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?



来源:https://stackoverflow.com/questions/5361220/empty-struct-in-c-vs-empty-struct-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!