__UNCONST(3)

πŸ“¦ version 11.0, in nixpkgs unstable

✨ __UNCONST β€” the compile-time cast-away macro, bestie edition ✨

okay so picture this: you're in __UNCONST(3) from the Library Functions Manual, and honestly? it's giving "tiny macro, big drama." let's get into it πŸ’…


🌸 NAME 🌸

__UNCONST β€” a compile time cast-away macro. yes, it literally casts away your const at compile time. no cap. 🫢


πŸŽ€ SYNOPSIS πŸŽ€

#include <sys/cdefs.h>

void
__UNCONST(x);

void
__UNVOLATILE(x);

two little functions-looking things, one iconic header. slay. ✨


πŸ’– DESCRIPTION πŸ’–

so here's the vibe check: the __UNCONST() macro is your go-to when you wanna omit warnings that certain compilers throw at you when you're operating with pointers declared with the const type qualifier in a context that doesn't have that qualifier. messy, i know. 😭

the classic examples, bestie:

  • passing a pointer declared with the const qualifier to a function that doesn't have such a qualifier
  • variable assignment from a const pointer to a non-const pointer

and in the same vein β€” like, same energy, same family β€” the __UNVOLATILE() macro can be used to exclude warnings related to the volatile type qualifier. it ate, honestly. πŸ”₯


🧚 IMPLEMENTATION NOTES 🧚

these macros are implemented by explicitly using unsigned long instead of intptr_t. and lowkey important: intptr_t is a signed type guaranteed to hold a pointer. so yeah, they chose the unsigned one on purpose. iykyk. πŸ’œ


πŸ¦‹ SEE ALSO πŸ¦‹

go read those, bestie, they're part of the lore. πŸ“š


πŸ’€ CAVEATS πŸ’€

okay this is the part where we get serious for a sec, because this is highkey a big deal:

both macros may hide valid errors, so their usage is not recommended unless there is a well-thought reason for a cast. like, an actual reason. not just "the compiler was being annoying." πŸ™ˆ
  • a typical use case for __UNCONST() involves an API that does not follow the so-called ``const correctness'' β€” even if it would be appropriate. yes, we're all side-eyeing that API. πŸ‘€
  • valid use cases of __UNVOLATILE() include passing a volatile pointer to memset(3).

also, and this is important: use of this macro is non-portable. it's part of the implementation namespace and should only be used in NetBSD code. periodt. πŸ‘‘


⭐ the cute sign-off ⭐

this little page came from Debian, dated October 17, 2013, and it's filed under __UNCONST(3). that's the whole footer, bestie β€” no notes, no cap, iconic behavior. πŸŽ€πŸŒˆπŸ’–

πŸ“¦ where this page comes from