why is a const array not accessible from a constexpr function?

前端 未结 1 1423
野的像风
野的像风 2021-01-21 00:37

i have a constexpr function named access, and i want to access one element from an array:

char const*const foo=\"foo\";
char const*const bar[10]={\"bar\"};

con         


        
相关标签:
1条回答
  • 2021-01-21 01:00

    The array needs to be declared constexpr, not just const.

    constexpr char const* bar[10]={"bar"};
    

    Without that, the expression bar[0] performs an lvalue-to-rvalue conversion in order to dereference the array. This disqualifies it from being a constant expression, unless the array is constexpr, according to C++11 5.19/2, ninth bullet:

    an lvalue-to-rvalue conversion unless it is applied to

    • a glvalue of literal type that refers to a non-volatile object defined with constexpr

    (and a couple of other exceptions which don't apply here).

    0 讨论(0)
提交回复
热议问题