`constexpr` variable “used in its own initializer”: Clang vs. GCC

后端 未结 1 1905
别那么骄傲
别那么骄傲 2021-02-15 09:05

This question seems related to an existing one, but I do not understand the \"portable workaround\" provided in the answer there (involving const auto this_ = this;

1条回答
  •  长情又很酷
    2021-02-15 09:54

    GCC is correct to reject the code (however the error message could use some work). You cannot use the address of a variable in a constant expression unless that variable has static storage duration.

    foo is not static. If you move it outside of main, things will work. Demo

    The line marked below is the problem:

    constexpr Test(const Test& src) noexcept
    : src_{&src} <--- That
    

    Standard reference: (Emphasis mine)

    [expr.const]

    A constant expression is either a glvalue core constant expression that refers to an entity that is a permitted result of a constant expression (as defined below), or a prvalue core constant expression whose value satisfies the following constraints:

    (5.2) — if the value is of pointer type, it contains the address of an object with static storage duration, the address past the end of such an object (8.7), the address of a function, or a null pointer value,

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