constexpr vs const vs constexpr const

天涯浪子 提交于 2019-12-01 08:11:13

From the comments it seems like OP is asking for Standard quote which defines const int as a compile-time constant, but const double as not.

The corresponding details are found in 5.19, Constant Expressions. In particular:

...an lvalue-to-rvalue conversion (4.1) unless it is applied to a non-volatile glvalue of integral or enumeration type that refers to a non-volatile const object with a preceding initialization, initialized with a constant expression...

int is an integral type, while double is not.

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.

The compiler does not allow an implicit narrowing or non-integral promotion during the initialisation of a constexpr variable.

This will work:

int main()
{
    const int PI1 = 3;
    constexpr int PI2 = 3;
    constexpr int PI3 = PI1;  // works
    static_assert(PI1 == 3, "");  // works

    const double PI1__ = 3;
    constexpr double PI2__ = 3;
    constexpr double PI3__ = double(PI1);  // works with explicit cast
    static_assert(PI2__ == 3, "");  // works now. PI1__ isn't constexpr
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!