简体   繁体   English

C ++与volatile和外部数据访问的Const-correctness

[英]C++ Const-correctness with volatile and external data acess

I'm an embedded C developer who has recently started messing with C++ code on an embedded device and am unsure about how const-correctness applies when a class accesses volatile data such as memory-mapped registers or data on an external device, such as an Analog-to-Digital Converter (ADC). 我是一名嵌入式C开发人员,他最近开始在嵌入式设备上搞乱C ++代码,当一个类访问外部设备(如内存映射寄存器或数据)等易失性数据时,我不确定如何应用const-correctness。模数转换器(ADC)。

For example, I have classes that interface to the device's hardware modules by accessing its memory-mapped registers through a pointer, like so: 例如,我有通过指针访问其内存映射寄存器来连接设备硬件模块的类,如下所示:

class IOPin
{
public:
    /* Constructor, destructor, other methods...*/

    // should this be a const method?
    bool ReadIOState() {return portregs_->state;}

private:
    /* Other private stuff...*/

    // Constructor points this to the right set of memory-mapped registers
    volatile struct portregs_t
    {
        uint32_t control;
        uint32_t state;
        uint32_t someotherreg;
    } *portregs_;
};

The register names are of course made up for the sake of example. 寄存器名称当然是为了举例说明。 I'm using a Microchip PIC32 device for anyone who's curious. 我正在为任何好奇的人使用Microchip PIC32器件。

From my possibly incorrect understanding, marking a method const means that the observable state of the object should not change as far as the caller is concerned. 根据我可能不正确的理解,标记方法const意味着对象的可观察状态不应该改变,只要涉及调用者。 So should the ReadIOState() method not be const because it accesses volatile data that could change at any time and thus the caller would observe the change? 那么ReadIOState()方法应该不是 const因为它访问可能随时改变的volatile数据,因此调用者会观察到这种变化吗? Or should it be const because the method isn't explicitly changing anything? 或者它应该是const因为该方法没有明确地改变任何东西?

Currently, I am leaning towards not making that method const for the reason stated in the question. 目前,由于问题中所述的原因,我倾向于不使该方法成为const This is especially true after stumbling upon this GotW article , which states that the meaning of const is changing to mean "able to read concurrently". 在找到这篇GotW文章后 ,尤其如此, 该文章指出const的含义正在改变为“能够同时阅读”。 My embedded application is single-threaded, but I suppose that could be a good litmus test for const ness in general. 我的嵌入式应用程序是单线程的,但我想这可能是一般的const测试的一个很好的试金石。

Also, how does the compiler treat const methods? 另外,编译器如何处理const方法? That is, what happens when I want to poll the state of the IO like this: 也就是说,当我想像这样轮询IO的状态时会发生什么:

// wait for IO pin to go high
while(!myIOpin.ReadIOState())
{}

If ReadIOState() is const , then can the compiler reuse the value returned after one call or is it smart enough to see that it is accessing volatile data and not do that? 如果ReadIOState()const ,那么编译器可以重用一次调用后返回的值,还是足够聪明地看到它正在访问volatile数据而不是那样做?

You are just having pointer to the struct inside the class and you do not change the pointer, so the method can be const. 您只是指向类中的结构,并且您不更改指针,因此该方法可以是const。 The compiler should not reuse the value from the previous call, it is smart enough. 编译器不应该重用前一次调用的值,它足够聪明。

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

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