Modules and opaque enum declarations
A pattern I've been taking advantage of since scoped enums were added is the ability to put opaque enum declarations in one header and the full definition in another header because it's more common to only need the size of the enum than it is to need to know any of the actual enumerators.
The main advantage of doing that is it is possible to add new enumerators without unnecessary recompilation. A translation unit that contains a function definition that accepts an enum type as an argument and simply passes to another function should not need to be recompiled if the type simply gained a new enumerator and this is easily accomplished with headers and opaque declarations.
Is this behavior possible to achieve with modules as well?
Posted Nov 13, 2022, 9:58 AM UTC
It sounds like you want to export a forward declaration of a scoped enum? Should be doable but messy and I wouldn't recommend it. In fact, I don't agree with the approach you've taken to using scoped enums here. Why wouldn't you want the users of your enum to know the valid values of the enum? Unfortunately, there's no way of doing that without including the definition
Because they don't need it.
In a real project I work on the most-referenced opaque enum is referred to in over 230 files. About half of those are header files so many of them would presumably not exist after a modularization of the code. Still that's over 100 source files that would persist after modularization. Less than 20% of them actually need to know the valid values so currently they do not see the definition and as a consequence do require recompilation when a new valid value is added. Most of these users are functions which accept the values as an argument and don't do anything with it except pass it to other functions as their arguments. I don't need to write input validation which would necessitate access to the definition for 100 different functions - a single is_valid() function is sufficient.
No matter how much modules allegedly improve incremental build times, it's not enough of an improvement to overcome recompiling 5x to 10x more translation units than necessary after a small change.