constexpr vs const vs constexpr const

前端 未结 3 1240
小鲜肉
小鲜肉 2021-01-14 13:41

const-vs-constexpr-on-variables

What the guy says about constexpr is right if double is used (or float of course). However, if you change t

3条回答
  •  一生所求
    2021-01-14 13:50

    Based on your answer in the comment this is my answer. The C++ standard makes it pretty clear. GCC 5.1 works pretty fine here though: https://godbolt.org/g/2oV6Hk

    A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and the implicit conversion sequence contains only § 5.20 134 c ISO/IEC N4567

    [...]

    (4.6) — integral promotions (4.5),

    (4.7) — integral conversions (4.7) other than narrowing conversions (8.5.4),

    [...]

    For the reference for narrowing conversions (8.5.4/7) in n4567:

    A narrowing conversion is an implicit conversion

    • from a floating-point type to an integer type, or
    • from long double to double or float, or from double to float, except where the source is a constant expression and the actual value after conversion is within the range of values that can be represented (even if it cannot be represented exactly), or
    • from an integer type or unscoped enumeration type to a floating-point type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type, or
    • from an integer type or unscoped enumeration type to an integer type that cannot represent all the values of the original type, except where the source is a constant expression and the actual value after conversion will fit into the target type and will produce the original value when converted back to the original type.

提交回复
热议问题