在 C++ 中执行非静态成员变量结构体/类需要被标记为易失性才能在成员函数中被视为易失性吗?

发布于 2024-10-14 21:14:45 字数 456 浏览 4 评论 0原文

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

我是否需要将 xy 声明为 易失性 还是将所有成员变量自动视为 易失性

我想确保“stuff with x”不会被编译器重新排序为“stuff with y”。

编辑: 如果我将普通类型转换为 易失性 类型,会发生什么情况?这是否会指示编译器不要重新排序对该位置的访问?我想将特殊情况下的普通变量传递给参数易失的函数。我必须确保编译器不会通过之前或之后的读取和写入重新排序该调用。

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?

I want to make sure that "stuff with x" is not reordered with "stuff with y" by the compiler.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自由如风 2024-10-21 21:14:45

标记成员函数易失性就像标记它const;这意味着接收者对象被视为被声明为易失性T*。因此,任何对 xy 的引用都将被视为成员函数中的 易失性 读取。此外,易失性对象只能调用易失性成员函数。

也就是说,如果您确实希望将对 xyy 的所有访问都视为 易失性。

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.

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.

唯憾梦倾城 2024-10-21 21:14:45

不必必须显式声明成员变量。

来自标准文档9.3.2.3

类似地,易失性语义 (7.1.6.1) 在访问对象及其非静态时应用于易失性成员函数
数据成员。

You don't have to declare the member variables explicitly..

From Standard docs 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.

孤凫 2024-10-21 21:14:45

以下代码:

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

使用 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>

并且由于 易失性 实例无法调用 非易失性 方法,我们可以假设,是的,xy 在方法中将是 易失性,即使 MyClass 的实例未声明 易失性< /代码>。

注意:如果需要,您可以使用 const_cast<> 删除 volatile 限定符;但是要小心,因为就像 const 一样,在某些情况下这样做可能会导致未定义的行为。

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:

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.

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.

所谓喜欢 2024-10-21 21:14:45

所以使用原来的例子:

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

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
    } 
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文