Take the address of a one-past-the-end array element via subscript: legal by the C++ Standard or not?

后端 未结 13 1531
傲寒
傲寒 2020-11-22 06:34

I have seen it asserted several times now that the following code is not allowed by the C++ Standard:

int array[5];
int *array_begin = &array[0];
int *ar         


        
13条回答
  •  故里飘歌
    2020-11-22 06:46

    C++ standard, 5.19, paragraph 4:

    An address constant expression is a pointer to an lvalue....The pointer shall be created explicitly, using the unary & operator...or using an expression of array (4.2)...type. The subscripting operator []...can be used in the creation of an address constant expression, but the value of an object shall not be accessed by the use of these operators. If the subscripting operator is used, one of its operands shall be an integral constant expression.

    Looks to me like &array[5] is legal C++, being an address constant expression.

提交回复
热议问题