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

前端 未结 6 1347
走了就别回头了
走了就别回头了 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:28

    5.19p2 does not define constant expressions, it defines core constant expressions.

    A core constant expression only becomes a constant expression if it conforms to one of the rules in 5.19p3. There, the relevant part was already pointed out by jrok:

    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.

    Your core constant expression &y does not evaluate to any of those, so it's not an address constant expression, and thus not a constant expression.

提交回复
热议问题