[英]What does this sentence mean in paragraph §3.2/2 of the C++11 Standard?
The sentence is part of paragraph §3.2/2 : 该句是第3.2 / 2段的一部分:
A variable whose name appears as a potentially-evaluated expression is odr-used unless it is an object that satisfies the requirements for appearing in a constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) is immediately applied . 名称显示为潜在评估表达式的变量是odr-used,除非它是满足出现在常量表达式(5.19)中的要求的对象,并且立即应用左值到右值转换(4.1) 。
What exactly does the sentence in bold mean above? 粗体句子究竟是什么意思?
Edit : 编辑 :
The answer to the question of which this one is considered a duplicate, doesn't say anything that could answer my question. 这个被认为是重复的问题的答案,并没有说任何可以回答我的问题。
It means that when you use a constant as a constant, it's just like you were actually using a constant. 这意味着当你使用常量作为常量时,就像你实际上使用常量一样。
struct S {
static const int i = 0;
};
int main() {
return S::i;
}
Although S::i
has an initialiser, it has no definition, but the text in your question makes a special exception for uses like this, where S::i
is only accessed for its value. 虽然S::i
有一个初始化器,但是它没有定义,但是你的问题中的文本对这样的用法做了一个特殊的例外,其中S::i
仅被访问它的值。 In that case, no definition is needed. 在这种情况下,不需要定义。
On the other hand, other uses do require a definition: 另一方面,其他用途确实需要定义:
struct S {
static const int i = 0;
};
int f(const int &i) {
return i;
}
int main() {
return f(S::i);
}
This program is invalid, and is accepted by some implementations, but rejected by others. 该程序无效,并被某些实现接受,但被其他人拒绝。 The call to f
requires an actual definition of S::i
to exist, although if f
gets inlined, it's possible for the lack of a definition to go undiagnosed. 对f
的调用需要存在S::i
的实际定义,但如果f
内联,则缺少定义可能未被诊断。
On my system, if compiling and linking the second program without optimisations, I get: 在我的系统上,如果没有优化地编译和链接第二个程序,我得到:
$ g++ test2.cc -o test2 /tmp/ccdEsfxs.o:test2.cc:function main: error: undefined reference to 'S::i' collect2: error: ld returned 1 exit status
To make it work, a definition needs to be provided, like so: 为了使其工作,需要提供定义,如下所示:
struct S {
static const int i = 0;
};
const int S::i;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.