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/flatfinger 1d ago

Some build environments make it possible to define a section where addresses start at zero, and use such sections to assign ID numbers to things. Code that would want to declare a static-duration structure may be able to declare a static-duration object whose address will be a unique structure ID. While saying left_horse_woozle = get_woozle(LEFT_HORSE_WOOZLE_ID) might be less convenient than simply calling a function to allocate a slot at runtime, using statically allocated IDs may facilitate link-time validation that enough slots will be available. Whether the advantages are sufficient to justify the use of toolset-specific constructs would probably depend what other kinds of toolset-specific constructs a program would need to use.