Why these expressions can not be evaluated during compliation?

Viewed 80

I write these code to get values of UTF-8 characters.

    constexpr const char* const chinese_chars = ",。!";
    constexpr uint32_t chinese_comma = (*(uint32_t *)(chinese_chars+0)) & 0x00ffffff;
    constexpr uint32_t chinese_peroid = (*(uint32_t*)(chinese_chars+3)) & 0x00ffffff;
    constexpr uint32_t chinese_exclamation = (*(uint32_t*)(chinese_chars+6)) & 0x00ffffff;

and the compiler reports an error say: Constexpr variable 'chinese_exclamation' must be initialized by a constant expression. It means the expression after = symbol is not a constant expression, but the C-style string chinese_chars is alreadly marked constexpr so it will (or 'maybe'?) not changed during runtime, and the expression does not contain variables or other things that can change during runtime, why compiler do not evaluate these expresions at compile time?

I have alternatively changed these expressions to

    const uint32_t chinese_comma = 0x8cbcef;  // denotes to a ,in UTF8
    const uint32_t chinese_peroid = 0x8280e3;  // denotes to a 。in UTF8
    const uint32_t chinese_exclamation = 0x81bcef; // denotes to a ! in UTF8

But I think the these two methods are identical.

0 Answers
Related