Modern C for C++ Peeps (2019)
floooh.github.io
floooh.github.io
I tend to disagree. Unless you are providing an API for third-party use and want to make your data types opaque to callers, do not ever use typedef with structs and pointers. A declaration like "struct foo *f" tells you two things - f itself is a pointer, and the pointed-to value is not a scalar (i.e. has named members). The "_t" suffix idiom is predominantly used by the standard library for integral scalars with certain properties and semantics, e.g. pid_t, uid_t, gid_t, time_t.
That said, typedefs/macros may be essential for portability concerns.
The solution to that problem is don't do that.
I agree with just using struct foo, as does the Linux style guide.
(Edit: I just noticed the page mentions this, but it is perhaps useful to reiterate.)
https://www.gnu.org/software/libc/manual/html_node/Reserved-...
typedef struct VkMemoryBarrier {
VkStructureType sType;
const void* pNext;
VkAccessFlags srcAccessMask;
VkAccessFlags dstAccessMask;
} VkMemoryBarrier;There is a good book written by Jens Gustedt [0] which is a great guide for C with all the new stuff. The book itself is CC licensed and code examples are MIT licensed. Jens is one of the good guys.
“cleanup (cleanup_function)
The cleanup attribute runs a function when the variable goes out of scope. This attribute can only be applied to auto function scope variables; it may not be applied to parameters or variables with static storage duration. The function must take one parameter, a pointer to a type compatible with the variable. The return value of the function (if any) is ignored.
If -fexceptions is enabled, then cleanup_function is run during the stack unwinding that happens during the processing of the exception. Note that the cleanup attribute does not allow the exception to be caught, only to perform an action. It is undefined what happens if cleanup_function does not return normally.”
— https://gcc.gnu.org/onlinedocs/gcc-11.1.0/gcc/Common-Variabl...
Why does the C committee keep wanting to make the language into C++?
Is this something that rank and file programmers actually want? Is this something that library writers are demanding?
At this point, I think I'm pretty fine with C anchoring down on features unless there is an absolutely pressing demand. There are enough newer languages experimenting in the space that I don't feel the need to pollute C with this stuff until those languages hash it all out.
A lot of embedded developers STRONGLY disagree with you.
There is a reason why embedded developers tend to turn off a whole bunch of C++ features. Note all the: "No RTTI. No templates. No exceptions." flags that embedded folks always engage.
Now that VSCode is getting actual support for embedded (see all the work the Rust Embedded guys did, for example) as well as official ARM support (Eclipse Theia), various languages can be used for development.
Thanks to this, people no longer need to force things because "If it's not in the C toolchain, nobody will support it." People don't need to torture C anymore to use their favorite language feature on their hardware of choice.
Yes, really we are.
So using handles can be a good idea, both in C and in C++, but it's really orthogonal to RAII. It can partially compoensate the lack of copy constructors and destructors, but if you need to keep a reference count to a handle you're still short of a good solution, and your code must be ready at any place to cope with a expired handle.