Re:Modern C for C++ Peeps (2019)
by visualradio on Wednesday May 26, 2021 @01:50PM
(#27293791)
If programmers do want to wrap structs in a typedef without using the "_t" suffix, the Vulkan API appears to have settled on upper-camel case:
typedef struct VkMemoryBarrier {
VkStructureType sType;
const void* pNext;
VkAccessFlags srcAccessMask;
VkAccessFlags dstAccessMask;
} VkMemoryBarrier;The _t suffix is also reserved by posix. (And by the C standard if the name starts with int or unit.)
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.)
Somewhat related; I feel like C reserves a lot of names that though different standards, is there a good list of all of those so I know to avoid them? (e.g. underscore prefixes)
This part of the GCC docs covers all of them, AFAIK:
https://www.gnu.org/software/libc/manual/html_node/Reserved-...
Agreed. If you typedef your struct, I can’t know if you wrapped it in a weird macro or if your data type is actually a renamed primitive. Once I gain familiarity with your codebase, it won’t matter, but it otherwise unnecessarily raises the barrier of entry.
That said, typedefs/macros may be essential for portability concerns.
> can’t know if you wrapped it in a weird macro
The solution to that problem is don't do that.
Huh. This is interesting, I can empathize with both sides of the argument. Are C text editors not able to take care of syntax highlighting the differences between a typedef'd scalar and a typedef'd struct? Colouring the type one way or another I figure would be sufficient.
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.