简体   繁体   English

易失性和const的成员函数

[英]Member functions that are volatile and const

I came across this post this which attempts to explain volatile member functions and const volatile member functions. 我碰到这个帖子其中试图解释volatile成员函数和const volatile成员函数。 The top answerer stated 最佳回答者表示

marking a member function as const or volatile (or a combined const volatile) applies those qualifiers to the this pointer used in the function 将成员函数标记为const或volatile(或组合的const volatile)会将这些限定符应用于函数中使用的this指针

What does the above mean ? 以上是什么意思? How does marking the this qualifier of a method as volatile or const volatile affect the this pointer? 将方法的this限定符标记为volatile或const volatile如何影响this指针?

I am confused what that would mean to the method(s) as such 我很困惑这对这样的方法意味着什么

class foo
{
   void someMethod() volatile 
   {std::cout << "volatile method" }

   void otherMethod() const volatile 
   {std::cout << "const volatile method"}
};

If this is marked volatile , it will affect the compiler's ability to optimise away updates to/from the members pointed to by *this . 如果this其标记为volatile ,则将影响编译器优化对*this指向的成员的更新/从中进行更新的能力。

The TYPICAL use-case for volatile is where you have hardware registers - not particularly typical to use a class to describe hardware registers, but we can do it. volatile的典型用例是您拥有硬件寄存器的地方-使用class来描述硬件寄存器不是特别典型,但是我们可以做到。

For example, one could have a class like this: 例如,可以有一个这样的类:

class my_hardware_device
{
  public:
    int32_t reg1; 
    int32_t reg2;
  private:
    int32_t reserved;
  public:
    int32_t reg4;
    int func() volatile;
};

volatile my_hardware_device hw1 = 
   reinterpret_cast<volatile my_hardware_device*>(0x10000000); 
...
int my_hardware_device::func() volatile
{ 
    reg2 = 3;
    reg3 = 8;
    reg2 = 7;
    if (reg2 == 4)
    {
       ...
    }
}

...
hw1->func();

Without volatile , the compiler could very well decide to remove the reg2 = 3 and determine that reg2 == 4 is always false, both because of reg2 = 7; 如果没有volatile ,则编译器可以很好地决定删除reg2 = 3并确定reg2 == 4始终为假,这都是因为reg2 = 7; . But since it's actually pointing at some hardware, it doesn't behave the way the compiler would expect. 但是由于它实际上是针对某些硬件的,因此它的行为不符合编译器的预期。 [This is a very silly example just constructed to show how it could work - I'm not by any means suggesting that this is a "correct" or "good" solution for any such hardware interface - never mind portability problems, and all manner of other things - and of course the vtable would cause complete havoc if you ever tried to use this with virtual functions - and if you are dealing with hardware in a driver, you probably do want vtables to solve hardware variants, giving yet another reason for implementing this sort of code in a different way.] [这是一个非常愚蠢的示例,只是为了说明它如何工作而设计的-我绝不是暗示这对于任何这样的硬件接口都是“正确”或“良好”的解决方案-不用担心可移植性问题,以及所有其他方式其他事情-当然,如果您尝试将vtable与虚函数一起使用,vtable将会造成彻底破坏-并且,如果您正在处理驱动程序中的硬件,则可能确实希望vtable解决硬件变体,这又给了另一个原因以不同的方式实现这种代码。]

The const variant means that the compiler is not allowed to write to members of *this (in other words members in the class foo in your example), except if they are marked as mutable (or if a const_cast is used to cast away the constness - const_cast can also be used to cast away volatileness, by the way). const变体意味着不允许编译器写入*this成员(换句话说,在您的示例中为foo类的成员),除非它们被标记为mutable (或者如果使用const_cast放弃const_cast ) -顺便说一下, const_cast也可以用来消除不稳定性。

Combining the both const and volatile simply means that reads can not be optimised away, and the compiler is not allowed to update the members of *this . constvolatile组合在一起仅意味着无法优化读取,并且不允许编译器更新*this的成员。

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

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