什么时候应该成员函数同时是const和volatile? [英] when should a member function be both const and volatile together?

查看:259
本文介绍了什么时候应该成员函数同时是const和volatile?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读关于volatile成员函数,并发现一个肯定的成员函数可以 const和volatile一起。我没有得到真正使用这样的东西。任何人都可以分享他们在实际使用的成员函数作为const和volatile一起的经验。

I was reading about volatile member function and came across an affirmation that member function can be both const and volatile together. I didn't get the real use of such a thing. Can anyone please share their experience on practical usage of having member function as const and volatile together.

我写了一个小类来测试一样:

I wrote small class to test the same:

class Temp
{
public:

    Temp(int x) : X(x)
    {
    }

    int getX() const volatile
    {
    	return X;
    }

    int getBiggerX()
    {
    	return X + 10;
    }
private:
    int X;
};

void test( const volatile Temp& aTemp)
{
    int x = aTemp.getX();
}

int main(int argc, char* argv[])
{
    const volatile Temp aTemp(10);
    test(aTemp);

    return 0;
}


推荐答案

的挥发性成员函数。好吧,我不能想到一个,因为唯一的情况,我可以想象是如此低级别,我不会考虑使用成员函数在第一位,而只是一个简单的结构与数据成员由易失性引用访问。

You asked for a practical example of volatile member functions. Well i can't think of one because the only situations i could imagine are so low-level that i would not consider using a member function in the first place, but just a plain struct with data-members accessed by a volatile reference.

但是,让我们把一个const volatile函数放进去,只是为了回答这个问题。假设您有一个地址为0x378h的端口,其中包含2个整数,每个4个字节。然后你可以写

However, let's put a const volatile function into it just for the sake of answering the question. Assume you have a port with address 0x378h that contains 2 integers, 4 bytes each. Then you could write

struct ints {
    int first;
    int second;
    int getfirst() const volatile {
        return first;
    }

    int getsecond() const volatile {
        return second;
    }
      // note that you could also overload on volatile-ness, just like
      // with const-ness
};

// could also be mapped by the linker. 
ints const volatile &p = *reinterpret_cast<ints*>(0x378L);

您正在说


不改变它们,但是这个抽象语义之外的另一个东西可以改变它。所以总是做一个真正的负载从它的地址。

I'm not changing them, but another thing outside this abstract semantics could change it. So always do a real load from its address.

实际上, volatile 表示对象的值可能不是最后存储在其中的值,实际上是未知的,并且可能在外部(不能由编译器观察)条件之间改变。所以当你从一个volatile对象读取时,编译器必须模拟精确的抽象语义,并且不执行优化:

Actually, volatile signals that the value of an object might not be the value last stored into it but is actually unknown and might have been changed in between by external (not observable by the compiler) conditions. So when you read from a volatile object, the compiler has to emulate the exact abstract semantics, and perform no optimizations:

a = 4;
a *= 2; 
  // can't be optimized to a = 8; if a is volatile because the abstract
  // semantics described by the language contain two assignments and one load.

下面已经确定了 volatile 一切都可以在标准的 1.9 中找到。它谈到的参数是实现定义的东西,像一些类型的sizeof。

The following already determines what volatile does. Everything can be found in 1.9 of the Standard. The parameters it talks about are implementation defined things, like the sizeof of some type.


本国际标准中的语义描述定义了一个参数化的非确定性抽象机。本国际标准不要求符合实施的结构。特别地,它们不需要复制或模拟抽象机的结构。相反,符合实现需要模拟(仅)抽象机器的可观察行为,如下所述。 [...]

The semantic descriptions in this International Standard define a parameterized nondeterministic abstract machine. This International Standard places no requirement on the structure of conforming implementations. In particular, they need not copy or emulate the structure of the abstract machine. Rather, conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below. [...]

执行良好构造的程序的一致性实现将产生与抽象机的对应实例的可能执行序列之一相同的可观察行为具有相同的程序和相同的输入。 [...]

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input. [...]

抽象机器的可观察行为是它对易失性数据的读取和写入以及对库I / O函数的调用。

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions.

这篇关于什么时候应该成员函数同时是const和volatile?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆