Home > Backend Development > C++ > Can C 14 Compilers Conformingly Treat Non-constexpr Standard Library Functions as `constexpr`?

Can C 14 Compilers Conformingly Treat Non-constexpr Standard Library Functions as `constexpr`?

Mary-Kate Olsen
Release: 2024-12-05 22:35:13
Original
232 people have browsed it

Can C  14 Compilers Conformingly Treat Non-constexpr Standard Library Functions as `constexpr`?

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.
Copy after login

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.);
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template