Constexpr pointer value

前端 未结 2 844
礼貌的吻别
礼貌的吻别 2020-11-28 11:00

I am trying to declare a constexpr pointer initialized to some constant integer value, but clang is foiling all my attempts:

Attempt 1:

constexpr int         


        
2条回答
  •  孤城傲影
    2020-11-28 11:35

    The reason is the one given by the (for once, very helpful) error message: reinterpret_cast is not allowed in a constant expression. It's listed as one of the explicit exception in 5.19 (paragraph 2).

    Changing the reinterpret_cast into a C-style cast still ends up with the semantical equivalent of a reinterpret_cast, so that doesn't help (and again the message is very explicit).

    If you had a way to obtain a pointer with value 0 you could indeed use p + 0xff but I can't think of a way to obtain such a pointer with a constant expression. You could have relied on the null pointer value (0 in a pointer context like you did, or nullptr) having a value of 0 on your implementation but as you've seen yourself your implementation refuses to do that. I think it's allowed to do that. (E.g. implementations are allowed to bail out for most constant expressions.)

提交回复
热议问题