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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:40:20  来源:igfitidea点击:

C++ volatile member functions

c++volatilemember-functions

提问by 0xbadf00d

class MyClass
{
    int x, y;
    void foo() volatile {
        // do stuff with x
        // do stuff with y
    }   
};

Do I need to declare xand yas volatileor will be all member variables treated as volatileautomatically?

我是否需要声明xy作为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 volatiletype? 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 volatileis 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 xor ywill be treated as a volatileread in the member function. Moreover, a volatileobject can only call volatilemember functions.

标记成员函数volatile就像标记它const;这意味着接收者对象被视为好像它被声明为volatile T*. 因此,对x或 的任何引用都y将被视为volatile成员函数中的读取。而且,一个volatile对象只能调用volatile成员函数。

That said, you may want to mark xand yvolatileanyway if you really do want all accesses to them to be treated as volatile.

这就是说,你可能要庆祝x,并yvolatile无论如何,如果你真的希望所有的访问给他们视为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 volatileinstance can't call a non-volatilemethod, we can assume that, yes, xand ywill be volatilein the method, even if the instance of MyClassis not declared volatile.

并且由于volatile实例不能调用non-volatile方法,我们可以假设,是的,x并且yvolatile在方法中,即使MyClass未声明的实例volatile

Note: you can remove the volatilequalifier using a const_cast<>if you ever need to; however be careful because just like constdoing 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 函数完全一样。