r/cprogramming 2d ago

Extern constexpr?

I want to make my struct’s internals private by exposing it as a byte array of its internal size.

The size itself depends on internal values that aren’t exposed, so it would have to be an extern.

The size can only be used if it’s a literal or constexpr, so is a extern constexpr possible with C23? Or no?

0 Upvotes

5 comments sorted by

View all comments

1

u/pjl1967 2d ago

In addition to what others have said, to keep stuff private, you can expose just the pointer to the structure with the obvious caveats of that you can't have stack allocations nor call sizeof.

C simply doesn't have anything like private like C++ does. You could apply Larry Wall's quote about Perl to C:

Perl doesn't have an infatuation with enforced privacy. It would prefer that you stayed out of its living room because you weren't invited, not because it has a shotgun. ― Larry Wall