Is taking the address of a local variable a constant expression in C++11?

前端 未结 6 1348
走了就别回头了
走了就别回头了 2021-02-07 06:13

The following C++11 program:

int x = 42;

void f()
{
        int y = 43;

        static_assert(&x < &y, "foo");
}

int main()
{
        f();         


        
6条回答
  •  没有蜡笔的小新
    2021-02-07 06:16

    Yes, you're missing the fact that, while y itself is initialised with a constant expression, that's not the same as &y.

    The address of y can vary a great deal depending on your call stack history.

    Paragraph 3 of C++11 5.19 Constant expressions states the conditions under which the address-of operator can be considered a constant expression (how core constant expressions detailed in paragraph 2 are allowed to morph into "real" constant expressions):

    ... An address constant expression is a prvalue core constant expression of pointer type that evaluates to the address of an object with static storage duration, to the address of a function, or to a null pointer value, or a prvalue core constant expression of type std::nullptr_t. Collectively, literal constant expressions, reference constant expressions, and address constant expressions are called constant expressions.

    Since &y is none of those things, it's not considered a constant expression.

提交回复
热议问题