多少使用“易失性”关键字有C ++函数,从语法的角度? [英] How many usage does "volatile" keyword have in C++ function, from grammar perspective?

查看:149
本文介绍了多少使用“易失性”关键字有C ++函数,从语法的角度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问这个函数基于这个概念(也许不正确?!):无论在哪里可以存在一个const,这个地方可以存在一个volatile。

I asked this function based on this concept (maybe incorrect?!): Wherever a const can exist, a volatile can exist at the place.

class classA
{
public:
    const int Foo() const;
}

这里第一个const表示返回值是const,更改。
第二个const表示是查询,此函数不能更改成员变量,不能调用非const函数。

Here the first "const" means the return value is const, we can not change it. the second const means "Is Query", this function can not change the member variable and can not call non-const function.

现在来到volatile:我可以理解什么volatile对变量,如volatile int a;
但是我不知道以下之间的区别:

Now comes to volatile: I can understand what volatile does on a variable, like "volatile int a;" However I have no idea of the difference among the following:

Case 1: The return type is volatile?
volatile void Function1();

Case 2: The function can only call volatile functions? Why add volatile here? Any example?
void Function2() volatile;

Case 3:   Is it valid? If yes, is it simply a combination of Case 1 and Case 2?
volatile void Function3() volatile;

当我们将const放在函数声明的末尾时,它有一个漂亮的名字:Is Query
在case 2中你能给volatile一个正确的名字/别名吗?
我的意思是,每当我们叫这个名字,我们可以知道我们正在谈论案例2,而不是案例1.

When we put the const at the end of function declaration, it has a beautiful name: "Is Query" Can you give a decent name/alias to the "volatile" in Case 2? I mean, whenever we call this name, we can know we are talking about Case 2, not case 1.

提前谢谢! >

Thank you in advance!

推荐答案

Volatile有一个主要功能,即STOP!此对象连接到外部资源,因此当我写入,不要相对于其他易失性读取或写入重新排序写入,当我读取它时,不要重新排序它,并且不优化这些离开!

Volatile has one primary function, which is to say "STOP! This object is connected to external resources, and thus when I write to it, don't reorder the write with respect to other volatile reads or writes, and when I read to it, don't reorder it likewise and don't optimize these away!".

为了支持这个功能,你可以在成员函数之后加上 volatile c $ c> volatile 类对象。

To support this, you can put volatile after member functions, which is necessary to call member functions on volatile class objects.

// just a silly example
struct HWOverlayClock {
  HWOverlayClock() { }

  int64_t getTime() volatile const { return timestamp; }

  int64_t timestamp;
};

// imagine we use an implementation defined way to put the
// object at some fixed machine address
volatile const HWOverlayClock clock __attribute__((at_address(0xbabe)));

在返回值上放置 volatile 但是在我看来,它似乎不太有用,因为返回值通常是临时的,而 volatile 语义在相反的临时规模。在 void 上放置 volatile 是特别不经意的( const 如果类型不是类类型(即在 * 的右侧),如果放在返回类型的顶级,则忽略code> volatile code>如果它是一个指针),因为这些返回值不对应于内存,可能也保存在寄存器中的实现)。将 volatile 放在非顶层引用或指针可能非常有用,如下面

Putting volatile on a return value can be done too, but it seems to me that it would be less useful since return values are usually temporaries and volatile semantics are quite on the the opposite scale of temporaries. Putting volatile on void is particularly nonsensical (const and volatile are ignored if put at the top-level of a return type if the type is not a class type (i.e at the right of a * if it is a pointer), because these return values do not correspond to memory, likely being kept in registers by implementations too). Putting volatile on non-toplevel for references or pointers can be useful, like in the following

struct Controller {
    HWOverlayClock volatile const* getClock() const { return clock; }

private:
    volatile const HWOverlayClock *clock;
};

希望它有帮助。

这篇关于多少使用“易失性”关键字有C ++函数,从语法的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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