Home > Backend Development > C++ > Is Treating Non-constexpr Standard Library Functions as `constexpr` Conforming to the C Standard?

Is Treating Non-constexpr Standard Library Functions as `constexpr` Conforming to the C Standard?

Linda Hamilton
Release: 2024-12-03 19:32:14
Original
267 people have browsed it

Is Treating Non-constexpr Standard Library Functions as `constexpr` Conforming to the C   Standard?

Treating Non-constexpr Standard Library Functions as Constexpr: A Conformance Analysis

The question arises whether a compiler extension that treats non-constexpr standard library functions as constexpr is conforming. Despite the permissiveness of the C 11 draft standard, which suggested such treatment could be allowed, subsequent developments have clarified the matter.

C 14 Explicit Proscription

In C 14, the draft standard section 17.6.5.6 explicitly states that implementations must not declare any standard library function signature as constexpr unless explicitly required. This ruling precludes the conforming treatment of non-constexpr standard library functions as constexpr.

GCC Extension

GCC has implemented an extension that treats certain built-in functions as constexpr. However, this extension is considered non-conforming based on the updated C 14 standard.

As-if Rule and Observable Behavior

It was initially unclear if the as-if rule permitted treating non-constexpr functions as constexpr. The as-if rule allows implementations to deviate from the standard's requirements as long as they produce equivalent observable behavior. However, altering the constexpr status of functions affects observable behavior, as demonstrated by SFINAE tests, which would behave differently when using identical code.

Conforming Mitigation

To make the GCC extension conforming, the compiler would need to issue warnings in strict conformance mode (e.g., -std=c 11 -pedantic) when non-constexpr standard library functions are used in constexpr contexts.

Intrinsics Exception

Compiler intrinsics are not covered by the standard and may be exempt from the constexpr function rule. Using built-in intrinsics (e.g., __builtin_cos) as constexpr could potentially be allowed.

The above is the detailed content of Is Treating Non-constexpr Standard Library Functions as `constexpr` Conforming to the C Standard?. 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