r/cprogramming • u/saul_soprano • Aug 03 '26
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?
1
Upvotes
3
u/SyntheticDuckFlavour Aug 03 '26 edited Aug 03 '26
externimplies connecting bits of code at link time, possibly across different translation units. Doing this withconstexpris not possible, because the compiler needs to have a complete view of the affected code within the same translation unit context in order to evaluate it at compile time.2
u/WittyStick Aug 03 '26 edited Aug 03 '26
constexprare essentiallystatic- you can't useextern constexpr- however you can useextern const.An issue with using a byte array is it will decay to a pointer, so you won't be able to pass around by value like a struct. You'd need a struct with an array member of the required size.
But this obviously won't work with
extern const size_t SIZE;I would advise against this approach anyway, since conversion from one kind of scalar struct to another is a strict aliasing violation and has potential issues for portability, alignment, padding etc.
We can convert a pointer to one struct to a pointer to another, if they're compatible, and it's common to do so - but we have to pass and return by pointer, and cannot pass or return a value of struct type if it is incomplete (ie, not defined in the header).
I have a technique to encapsulate a struct's fields whilst still allowing it to be passed and returned by value, and allowing its
sizeof()to be taken, but requiring a few GCC extensions (optional - so the code will compile with other compilers, but won't provide the encapsulation). If you give an example of your struct I can give a demonstration.1
u/pjl1967 Aug 03 '26
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
privatelike C++ does. You could apply Larry Wall's quote about Perl to C:1
u/flatfinger Aug 03 '26
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.1
u/DawnOnTheEdge Aug 07 '26
C23 does not have the
inline constexprspecifier of C++17, which would let the identical declarations in a header included by multiple files be merged at link time. However, some compilers have a flag that will merge all globalconstexprdefinitions with the same name.A
constexprdeclaration must always be defined before use. If you want to be able to refer to an immutable object with an unknown definition, useextern const.