简体   繁体   English

这句话在C ++ 11标准的第3.2.2段中意味着什么?

[英]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.

相关问题 这个C ++ / C ++ 11构造意味着什么? - What does this C++ / C++11 construction mean? T&&(双与号)在 C++11 中是什么意思? - What does T&& (double ampersand) mean in C++11? C ++标准的未定义行为段落中的[注]是什么意思? - What does the [Note] in undefined behavior paragraph in C++ standard mean? C ++ 11中“return {}”语句的含义是什么? - What does “return {}” statement mean in C++11? extern模板是c ++ 11扩展的意思是什么? - What does extern templates are a c++11 extension mean? 在C ++ 11中thread_local是什么意思? - What does the thread_local mean in C++11? 这个陈述在C ++ 11标准中的含义是什么? - What is the meaning of this statement in the C++11 Standard? 什么是C ++ 11标准中的使用操作? - What is a consume operation in the C++11 Standard? C ++ 11标准中的哪一个子句允许我消除下面`A :: operator-()`中的`return`语句中的`A`? - What clause in the C++11 Standard does allow me to eliminate the `A` in the `return` statement in the `A::operator-()` below? “E”代表C ++ 11标准“ISO / IEC 14882:2011(E)”的名称 - What does “E” stand for in the name of C++11 standard “ISO/IEC 14882:2011(E)”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM