News for hackers. Stuff that matters.
Re:Why Not Just Do Simple C++ RAII in C? by rkangel on Thursday May 23, 2024 @06:13AM (#40452804)
> Using C-like structs is a very common use case

Not true. Using C structs themselves in C++ is very common - when you include the C header file, the relevant declarations are wrapped in "extern "C" {}" which gives structs C semantics. You can do this because C++ is backwards compatible with C.

Most of the time when you use a struct in C++ you're just ignoring most of the capabilities of objects (which is fine!). If you declare a struct in C++, you're getting an object. The only difference between the struct and class keywords in C++ is the default privacy of the members.

Re:Why Not Just Do Simple C++ RAII in C? by gpderetta on Thursday May 23, 2024 @09:37AM (#40454566)
A C++ structure has exactly the equivalent semantics of a C structure if it exists. Extern "C" only affects linkage of functions. It has no effect on structure definitions.
Re:Why Not Just Do Simple C++ RAII in C? by OskarS on Thursday May 23, 2024 @09:48AM (#40454685)
It absolutely does not. A C++ structure is a much richer object: it can have custom copying, moving and assignment behaviours, it can have vtables, it has RAII, etc. That's the whole point about the "object model" in the article: C++ has it, C does not.

What I think you're trying to say is "a POD structure with no custom behavior is essentially identical in C and C++". That is mostly true, though if the struct contains a union, C++ has stricter UB rules (there might be other differences as well, but that's the one I can think of at the moment).

Re:Why Not Just Do Simple C++ RAII in C? by gpderetta on Thursday May 23, 2024 @11:01AM (#40455586)
What I'm saying is that extern "C" has no effect on structure compatibility.