【发布时间】:2020-03-06 18:49:15
【问题描述】:
根据https://docs.microsoft.com/en-us/cpp/cpp/constexpr-cpp?view=vs-2019
"constexpr 表示该值或返回值是常量,并且, 如果可能,在编译时计算。”
另外,Difference between constexpr and static constexpr global variable
"在变量声明中,constexpr 隐含 const,而 const 默认情况下,命名空间范围内的变量具有内部链接(因此添加 static 不会改变任何东西)。”
我认为这意味着constexpr 意味着const 意味着static。
但是,这个问题的答案让我很困惑:When and why would you use static with constexpr?
它表明
- “constexpr 变量不是编译时值”
- “看起来,在某些极端情况下,我们可以从静态 constexpr 变量的静态存储持续时间中受益。”
我误会了什么?
【问题讨论】:
-
"
constexpr暗示const暗示static"。不,constexpr确实暗示了const(但不一定是相反的)。const并不暗示static。 -
@Peter
const确实暗示了static(即内部链接)。但仅在命名空间范围内。例外情况适用。