Is Treating Non-Constexpr Standard Library Functions as Constexpr a Conforming Compiler Extension?
Current C standards mandate that standard library functions be declared as constexpr only when explicitly required. However, earlier drafts suggested otherwise.
C 11 and the Draft Standard
During C 11's development, a proposal aimed to explicitly allow treating non-constexpr standard library functions as constexpr. GCC's decision to support this feature in strict conformance mode was based on this draft.
Shifting Consensus in C 14
However, the consensus shifted during C 14's development. The proposal was amended, and the final standard states that implementations are prohibited from declaring any standard library function constexpr unless explicitly required.
This change stems from concerns about divergent behavior across implementations, as SFINAE could leverage this to produce different results for identical code.
Conformance for GCC
GCC's current implementation for this case is a non-conforming extension, as it violates the C 14 standard. To achieve conformance, GCC would need to issue warnings or disable this feature in strict conformance mode.
Intrinsics
Compiler intrinsics, unlike standard library functions, are exempt from this rule since they are not covered by the standard. Therefore, treating intrinsics as constexpr is generally allowed.
The above is the detailed content of Is GCC's Constexpr Treatment of Non-Constexpr Standard Library Functions a Conforming Extension?. For more information, please follow other related articles on the PHP Chinese website!