使一个结构变体使其所有成员挥发性? [英] Does making a struct volatile make all its members volatile?

查看:82
本文介绍了使一个结构变体使其所有成员挥发性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有:

If I have:

struct whatever {
int data;
};
volatile whatever test;


will test.data be volatile too?


test.data

解决方案

另一个问题可以问(或者只是另一种方式来看看原始问题):

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 with const then all its members will be const too

同样,如果你声明一个类型任何 c> volatile ,那么它的所有成员也将是 volatile ,就像你声明的对象 const ,所有它的成员也将 const

Similarly, if you declare an object of type whatever with volatile then all its members will be volatile too, just like if you declare the object with const, all it's member will be const too.

const volatile 是同一硬币的两个面;他们是这样,标准经常将他们称为 cv-qualifiers

const and volatile are two faces of the same coin; they're so that the Standard often refers to them as cv-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?)

相关主题:

我们为什么要使用C ++中的volatile关键字?

这篇关于使一个结构变体使其所有成员挥发性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆