Stateful Metaprogramming: An Ill-Formed Threat in C
The enigmatic concept of stateful metaprogramming, exemplified by the "constexpr counter," has intrigued and perplexed C programmers. While it appears permissible in C 14, its status in C 17 remains uncertain.
The Mysterious Constexpr Counter
As described in the post, the constexpr counter leverages the ADL mechanism to capture and retrieve stateful information. By defining a friend function within a template and referencing it later, it enables the storage of metaprogramming state.
Contradiction and Controversy
This technique directly contradicts previous guidelines on storing states in metaprogramming. It raises questions about the validity of such arcane constructs.
CWG's Active Issue
The C Working Group (CWG) has recognized this issue (CWG Active Issue 2118). They have unanimously agreed that these techniques should be ill-formed, but the mechanism for prohibiting them remains undetermined.
Implications for C 17
While CWG has not definitively declared stateful metaprogramming ill-formed, it is highly likely that such a prohibition will be retroactively applied through a defect report (DR). Until then, its legality under C 14 and its status in future C standards remain uncertain.
Conclusion
Stateful metaprogramming, while intriguing, is a potential threat to the consistency and reliability of C metaprogramming. CWG's active pursuit of an ill-formation mechanism underscores the need for caution when employing these advanced techniques. As C evolves, we can expect a resolution to this issue that will clarify the role of stateful metaprogramming in the programming landscape.
The above is the detailed content of Is Stateful Metaprogramming a Threat to C Consistency?. For more information, please follow other related articles on the PHP Chinese website!