Home > Article > Backend Development > Is Stateful Metaprogramming Legal in C 17?
In the realm of metaprogramming, arises the intriguing technique of constexpr counters, known as stateful metaprogramming. This intricate technique has sparked both admiration and unease among developers. The question lingers: with the advent of C 17, has its legality been revisited?
Stateful Metaprogramming in C 14
As the provided code snippet demonstrates, stateful metaprogramming in C 14 leverages friend declarations, ADL, and constexpr to create a counter that maintains its state across invocations. Despite its seemingly arcane nature, this technique is legal under C 14.
The Dark Cloud: CWG Issue 2118
However, this practice has drawn scrutiny from the C standards committee, specifically through active issue CWG issue 2118. The committee recognizes the obscurity of this technique and advocates for its demarcation as ill-formed.
C 17's Silent Stance
Despite the CWG's concern, C 17 does not explicitly address this issue. This implies that stateful metaprogramming remains legal in C 17, albeit with a warning that its fate may be altered in the future.
Retroactive Implications
Should a prohibition mechanism be devised, it is possible that stateful metaprogramming may be retroactively deemed ill-formed. This would render the code snippet presented in the question invalid, posing potential risks for existing codebases.
Conclusion
The future of stateful metaprogramming remains uncertain. While it is still legal in C 17, the CWG's discomfort with the technique suggests that its days may be numbered. Developers who employ this technique should be aware of this potential caveat and monitor future updates in the C standard to ensure their code's integrity.
The above is the detailed content of Is Stateful Metaprogramming Legal in C 17?. For more information, please follow other related articles on the PHP Chinese website!