3

I know in C++ the struct keyword can be safely omitted for structures, but I'm not sure if the same rule can be safely apply to the structures inherited from C headers. To be more specific, those structures form socket interface, e.g., sockaddr_in, sockaddr_in6...etc

So, can I safely omit struct keyword for all the structures including those from C interfaces when I'm using C++?

I've done some searching on this one, but can't find any clear answer.

  1. Here is a similar question, but did not mention those old C structure Why does C need "struct" keyword and not C++?
  2. Another one here C++ struct keyword usage, but the question did not received a detailed or sure answer.

I did try to take out the struct keyword from one of my C++ source and it compiled without any error. But I would like to know if it's safe in all cases.

user2535650
  • 265
  • 1
  • 2
  • 8
  • If you're compiling the code as C++ then the `struct` keyword is optional. That's all there is to it. Maybe I don't understand the question. – Indiana Kernick Jul 09 '19 at 02:56
  • 3
    Short answer: yes. In C++, the `struct` keyword is optional when creating an instance of a `struct` type (except in a couple of rare cases to avoid ambiguity, such as a `struct` type and a function having the same name). If the code is intended to be compiled as both C and C++, then the `struct` keyword is needed for the C compilation to succeed. – Peter Jul 09 '19 at 03:05
  • @Peter I never thought of the C compilation aspect, thank you for that. The code I'm writing right now is only meant to be compiled with C++ compiler, so I guess I'll omit it then. – user2535650 Jul 09 '19 at 03:13
  • @Kerndog73 You did not misunderstand the question. I guess I'm just try to be extra sure that `struct` keyword can be omitted even they're from C style headers. But come to think of it, those C style headers will be processed by the C++ compiler as well, so the C++ compiler should take care of it. – user2535650 Jul 09 '19 at 03:17
  • 2
    As C++ has evolved out of C, it is still possible to write code which works in both languages. (This doesn't include that it means exactly the same in both languages.) C `struct`s belong to them. The compiler doesn't know whether you wrote this code for C or C++. If you compile with `g++` it will read any C header as C++ header and apply the syntax and semantic rules of C++. So, you can omit the `struct` keyword to use a `struct` in C++ which was actually part of a C API. – Scheff's Cat Jul 09 '19 at 05:41
  • 1
    Another note about `struct`s in C vs. C++: In C, there are e.g. [Flexible Array Members](https://en.wikipedia.org/wiki/Flexible_array_member) (allowed since C99) but not standardized in C++. In opposition, C++ allows member functions in `struct`s. So, there is a only a certain subset of `struct`s which works in both languages. I'm not sure about packing and alignment - I would keep an eye on this if this issue would occur in daily business. – Scheff's Cat Jul 09 '19 at 05:49

0 Answers0