使一个结构变体使其所有成员挥发性? [英] Does making a struct volatile make all its members volatile?
问题描述
如果我有:
If I have:
struct whatever { int data; };
volatile whatever test;will
test.data
be volatile too?
将test.data $ c $
解决方案另一个问题可以问(或者只是另一种方式来看看原始问题):
Another question can be asked (or simply another way to look at the original question):
如果我有:
struct whatever { int data; }; const whatever test;
还要test.data是
const
Will test.data be
const
too?我的回答是:是的。如果你用
const
声明一个类型为的对象 c>,那么它的所有成员将是
const
太My answer is : Yes. If you declare an object of type
whatever
withconst
then all its members will beconst
too同样,如果你声明一个类型
任何
c> volatile ,那么它的所有成员也将是volatile
,就像你声明的对象const
,所有它的成员也将const
。Similarly, if you declare an object of type
whatever
withvolatile
then all its members will bevolatile
too, just like if you declare the object withconst
, all it's member will beconst
too.
const
和
volatile
是同一硬币的两个面;他们是这样,标准经常将他们称为cv-qualifiers
。
const
andvolatile
are two faces of the same coin; they're so that the Standard often refers to them ascv-qualifiers
.从标准引用($ 7.1.5.1 / 8)
Quoting from the Standard ($7.1.5.1/8)
[注意:volatile是
实现以避免积极的
优化涉及对象
,因为对象的值可能
通过
实现无法检测的方式改变。 1.9详细的
语义。一般来说,volatile的语义
意味着C ++中的
与C中的相同。][Note: volatile is a hint to the implementation to avoid aggressive optimization involving the object because the value of the object might be changed by means undetectable by an implementation. See 1.9 for detailed semantics. In general, the semantics of volatile are intended to be the same in C + + as they are in C. ]
这意味着,如果你的对象是一个结构体的实例,那么编译器不能避免涉及对象的强烈优化,除非它避免了每个的会员。 (否则,如何避免涉及对象的优化?)
That means, if your object is an instance of a struct, then the compiler cannot avoid aggressive optimization involving the object, unless it avoids aggressive optimization of each of it's members. (Otherwise, how else it can avoid optimization involving the object?)
相关主题:
这篇关于使一个结构变体使其所有成员挥发性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!