Why do we need the two definitions: integral constant expression and converted constant expression?

前端 未结 3 1982
春和景丽
春和景丽 2021-02-04 03:49

§5.19/3 in C++14 defines an integral constant expression and a converted constant expression:

An integral constant expression is an expression

3条回答
  •  梦谈多话
    2021-02-04 04:14

    After discussion of answers provided by Jerry Coffin and cpplearner, let me propose to rewrite those damn rules, like this:

    [expr.const] 5.20\3 (modified)

    An integral constant expression is an expression of integral or unscoped enumeration type, that is implicitly converted to a prvalue core constant expression of the same type such that the implicit conversion sequence contains only an lvalue-to-rvalue conversion.

    [expr.const] 5.20\4 (modified)

    A converted constant expression of type T is an expression of any type, that is implicitly converted to a constant expression of type T such that the implicit conversion sequence contains only:

    • user-defined conversions,
    • lvalue-to-rvalue conversions,
    • array-to-pointer conversions,
    • function-to-pointer conversions,
    • qualification conversions,
    • integral promotions,
    • integral conversions other than narrowing conversions,
    • null pointer conversions from std::nullptr_t,
    • null member pointer conversions from std::nullptr_t, and
    • function pointer conversions,

    and where the reference binding (if any) binds directly. [ Note: such expressions may be used in new expressions, as case expressions, as enumerator initializers if the underlying type is fixed, as array bounds, and as non-type template arguments. — end note ]

    Now the difference is obvious, uh? Also it should be reminded that according to 5.20\7; 4\5 an expression of literal class type may be used instead of integral constant expression in some cases.

提交回复
热议问题