Conforming Compiler Extension for constexpr Handling of Non-constexpr Standard Library Functions?
In C 11, the draft standard appeared to allow treating standard library functions as constexpr even if they were not explicitly marked as such. However, this stance has since evolved.
C 14 Evolution
In C 14, it's explicitly stated that non-standard-required functions should not be declared as constexpr by implementations. This is outlined in section 17.6.5.6 of the C 14 draft standard:
An implementation shall not declare any standard library function signature as constexpr except for those where it is explicitly required.
This decision was made to prevent implementation divergence, particularly with SFINAE usage leading to different observable behavior.
GCC's Implementation
GCC previously treated certain non-constexpr standard library functions as constexpr based on the earlier LWG 2013 proposal resolution. However, this behavior is no longer considered conforming in C 14.
Warning Absence in Strict Mode
Despite the nonconformity, GCC does not generate warnings in strict conformance mode (-std=c 11 -pedantic). This is likely an oversight that will be addressed in future updates.
Intrinsics Exemption
Compiler intrinsics are not subject to the same rule as standard library functions. Therefore, using intrinsics like:
static constexpr double a = __builtin_cos(3.);
should be considered conforming.
Conclusion
Treating non-constexpr standard library functions as constexpr is currently a non-conforming extension in C 14. While GCC allowed this in C 11 based on an earlier proposal resolution, it's expected that this extension will be removed or modified to conform to the current C 14 standard.
The above is the detailed content of Can C 14 Compilers Conformingly Treat Non-constexpr Standard Library Functions as `constexpr`?. For more information, please follow other related articles on the PHP Chinese website!