简体   繁体   English

C++ volatile 成员函数

[英]C++ volatile member functions

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?我需要将xy声明为volatile还是将所有成员变量都自动视为volatile

I want to make sure that "stuff with x " is not reordered with "stuff with y " by the compiler.我想确保编译器不会将“带有x东西”与“带有y东西”重新排序。

EDIT: What happens if I'm casting a normal type to a volatile type?编辑:如果我将普通类型转换为volatile类型会发生什么? 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.我必须确保编译器不会使用先前或跟随的读取和写入重新排序该调用。

Marking a member function volatile is like marking it const ;将成员函数标记为volatile就像将其标记为const it means that the receiver object is treated as though it were declared as a volatile T* .这意味着接收器对象被视为被声明为volatile T* Consequentially, any reference to x or y will be treated as a volatile read in the member function.因此,对xy任何引用都将被视为成员函数中的volatile读取。 Moreover, a volatile object can only call volatile member functions.而且,一个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 .也就是说,如果您确实希望对xy所有访问都被视为volatile ,则无论如何您可能希望将它们标记为volatile

You don't have 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 成员函数。

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方法,我们可以假设,是的, xy在方法中将是volatile ,即使MyClass的实例未声明为volatile

Note: you can remove the volatile qualifier using a const_cast<> if you ever need to;注意:如果需要,您可以使用const_cast<>删除volatile限定符; however be careful because just like const doing so can lead to undefined behavior under some cases.但是要小心,因为就像const那样在某些情况下会导致未定义的行为。

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
    } 
};

IBM暗示它的工作方式与const函数完全相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM