Home > Backend Development > C++ > Is GCC's Constexpr Treatment of Non-Constexpr Standard Library Functions a Conforming Extension?

Is GCC's Constexpr Treatment of Non-Constexpr Standard Library Functions a Conforming Extension?

Susan Sarandon
Release: 2024-11-30 01:49:09
Original
751 people have browsed it

Is GCC's Constexpr Treatment of Non-Constexpr Standard Library Functions a Conforming Extension?

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!

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