C++ volatile 成员函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4826719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ volatile member functions
提问by 0xbadf00d
class MyClass
{
int x, y;
void foo() volatile {
// do stuff with x
// do stuff with y
}
};
Do I need to declare x
and y
as volatile
or will be all member variables treated as volatile
automatically?
我是否需要声明x
和y
作为volatile
或将所有成员变量视为volatile
自动处理?
I want to make sure that "stuff with x
" is not reordered with "stuff with y
" by the compiler.
我想确保编译器x
不会将“stuff with ”重新排序为“stuff with y
”。
EDIT:
What happens if I'm casting a normal type to a volatile
type? Would this instruct the compiler to not reorder access to that location? I want to pass a normal variable in a special situation to a function which parameter is volatile. I must be sure compiler doesn't reorder that call with prior or followed reads and writes.
编辑:如果我将普通类型转换为volatile
类型会发生什么?这会指示编译器不要重新排序对该位置的访问吗?我想在特殊情况下将普通变量传递给参数可变的函数。我必须确保编译器不会使用先前或跟随的读取和写入重新排序该调用。
采纳答案by templatetypedef
Marking a member function volatile
is like marking it const
; it means that the receiver object is treated as though it were declared as a volatile T*
. Consequentially, any reference to x
or y
will be treated as a volatile
read in the member function. Moreover, a volatile
object can only call volatile
member functions.
标记成员函数volatile
就像标记它const
;这意味着接收者对象被视为好像它被声明为volatile T*
. 因此,对x
或 的任何引用都y
将被视为volatile
成员函数中的读取。而且,一个volatile
对象只能调用volatile
成员函数。
That said, you may want to mark x
and y
volatile
anyway if you really do want all accesses to them to be treated as volatile
.
这就是说,你可能要庆祝x
,并y
volatile
无论如何,如果你真的希望所有的访问给他们视为volatile
。
回答by liaK
You don'thave to declare the member variables explicitly..
您不必显式声明成员变量..
From Standard docs 9.3.2.3,
从标准文档9.3.2.3,
Similarly, volatile semantics(7.1.6.1) apply in volatile member functions when accessing the object and its nonstatic data members.
类似地,当访问对象及其非静态数据成员时,volatile 语义(7.1.6.1)适用于 volatile 成员函数。
回答by ereOn
The following code:
以下代码:
#include <iostream>
class Bar
{
public:
void test();
};
class Foo
{
public:
void test() volatile { x.test(); }
private:
Bar x;
};
int main()
{
Foo foo;
foo.test();
return 0;
}
Raises an error upon compilation with gcc:
使用 gcc 编译时引发错误:
main.cpp: In member function 'void Foo::test() volatile':
main.cpp:14:33: error: no matching function for call to 'Bar::test() volatile'
main.cpp:7:8: note: candidate is: void Bar::test() <near match>
And since a volatile
instance can't call a non-volatile
method, we can assume that, yes, x
and y
will be volatile
in the method, even if the instance of MyClass
is not declared volatile
.
并且由于volatile
实例不能调用non-volatile
方法,我们可以假设,是的,x
并且y
将volatile
在方法中,即使MyClass
未声明的实例volatile
。
Note: you can remove the volatile
qualifier using a const_cast<>
if you ever need to; however be careful because just like const
doing so can lead to undefined behavior under some cases.
注意:如果需要,您可以volatile
使用 a删除限定符const_cast<>
;但是要小心,因为const
在某些情况下,这样做可能会导致未定义的行为。
回答by ianeperson
So using the original example:
所以使用原始示例:
class MyClass
{
int x, y;
void foo() volatile {
// do stuff with x
// do stuff with y
// with no "non-volatile" optimization of the stuff done with x, y (or anything else)
}
void foo() {
// do stuff with x
// do stuff with y
// the stuff done with x, y (and anything else) may be optimized
}
};
回答by Flexo
IBM impliesit works exactly like const functions.
IBM 暗示它的工作方式与 const 函数完全一样。