简体   繁体   English

从语法角度看,“volatile”关键字在C ++函数中有多少用法?

[英]How many usage does “volatile” keyword have in C++ function, from grammar perspective?

I asked this function based on this concept (maybe incorrect?!): Wherever a const can exist, a volatile can exist at the place. 我基于这个概念问了这个函数(可能不正确?!):只要const存在,就可以在这个地方存在一个volatile。

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

Here the first "const" means the return value is const, we can not change it. 这里第一个“const”表示返回值是const,我们无法改变它。 the second const means "Is Query", this function can not change the member variable and can not call non-const function. 第二个const表示“Is Query”,这个函数不能改变成员变量而不能调用非const函数。

Now comes to volatile: I can understand what volatile does on a variable, like "volatile int a;" 现在变得不稳定:我可以理解volatile对变量的作用,比如“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;

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? 当我们将const放在函数声明的末尾时,它有一个漂亮的名字:“Is Query”你能为Case 2中的“volatile”提供一个合适的名字/别名吗? I mean, whenever we call this name, we can know we are talking about Case 2, not case 1. 我的意思是,每当我们称这个名字时,我们都知道我们在谈论案例2,而不是案例1。

Thank you in advance! 先感谢您!

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有一个主要功能,就是说“STOP!这个对象连接到外部资源,因此当我写入它时,不要对其他易失性读取或写入重新排序,当我读取它时,不要同样重新排序,不要优化它们!“

To support this, you can put volatile after member functions, which is necessary to call member functions on volatile class objects. 为了支持这一点,您可以在成员函数之后放置volatile ,这是在volatile类对象上调用成员函数所必需的。

// 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)));

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. volatile放在返回值上也可以这样做,但在我看来它不太有用,因为返回值通常是临时值, volatile语义恰好与临时值相反。 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 (ie 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). volatile放在void是特别荒谬的(如果类型不是类类型(如果它是指针的话,在*的右侧),如果放在返回类型的顶层,则忽略constvolatile ,因为这些返回值与内存不对应,也可能由实现保存在寄存器中)。 Putting volatile on non-toplevel for references or pointers can be useful, like in the following volatile放在非顶层的引用或指针上可能很有用,如下所示

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

private:
    volatile const HWOverlayClock *clock;
};

Hope it helps. 希望能帮助到你。

The second const means "Is Query", this function can not change the member variable and can not call non-const function. 第二个const表示“Is Query”,这个函数不能改变成员变量而不能调用非const函数。

Yes, thats correct. 对,那是正确的。

volatile void Function1(); volatile void Function1();
The return type is volatile? 返回类型是不稳定的?

The return type of the function here is void (means nothing). 这里函数的返回类型是void (没有任何意义)。 If it returns nothing, it makes no sense to specify that the nothing being returned be volatile . 如果它什么也没有返回,则指定返回的任何内容都不是volatile是没有意义的。 The volatile preceeding void will be reduntant. volatile先前空白将是还原剂。

If the return type was int , then yes the volatile preceeding it applies to the return type. 如果返回类型是int ,那么它之前的volatile应用于返回类型。 It means that this return value can only be taken in a variable type which is volatile . 这意味着此返回值只能采用volatile的变量类型。

void Function2() volatile; void Function2()volatile;
The function can only call volatile functions? 该函数只能调用volatile函数吗? Why add volatile here? 为什么在这里添加volatile? Any example? 任何例子?

Yes, thats correct. 对,那是正确的。 See the Code Example below which demonstrates this. 请参阅下面的代码示例,它演示了这一点

volatile void Function3() volatile; volatile void Function3()volatile;
Is it valid? 有效吗? If yes, is it simply a combination of Case 1 and Case 2? 如果是,它只是案例1和案例2的组合吗?

The volatile in Case 1, is redundant, If a function is returning void (nothing) it makes no sense that the returned value be volatile , So essentially above is equivalent to just case 2. volatile案例1中是多余的,如果一个函数返回void (无)它没有任何意义,返回值是volatile ,所以基本上上面仅相当于2的情况下。

An sample Example : 示例示例

#include<iostream>

class Myclass
{
    public:
        volatile int i;
        Myclass():i(10){} 
        void doSomething()
        {
            std::cout<<"\nInside doSomething";
        }
        void doSomethingMore() volatile
        {
            std::cout<<"\nInside doSomethingMore";
            doSomething();   //Error


        }

};

int main()
{
     Myclass obj;
     obj.doSomethingMore();

     return 0;
}

"Is Query" Can you give a decent name/alias to the "volatile" in Case 2? “查询”你能否为案例2中的“易变”提供一个合适的名称/别名? I mean, whenever we call this name, we can know we are talking about Case 2, not case 1. 我的意思是,每当我们称这个名字时,我们都知道我们在谈论案例2,而不是案例1。

Follow the simple rule: 遵循简单的规则:
Whenever the volatile or the const keywords appear at the end of the function signature in declaration or definition the keyword applies to the function. 只要volatileconst关键字出现在声明或定义中函数签名的末尾,该关键字就适用于该函数。

This is because anything that preceeds the function signature applies to the return type. 这是因为函数签名之前的任何内容都适用于返回类型。

It make more sense to use the volatile keyword preeceding the function signature because it exactly indicates the purpose, I think you should stick to this standard way because it demonstrates the intent more clearly than any alias would. 使用volatile关键字优先于函数签名更有意义,因为它准确地表明了目的,我认为你应该坚持这种标准方式,因为它比任何别名都更清楚地表明了意图。

the second const means "can be used when this is a pointer to const " , this function can not change non- mutable member variables of this and can not call non-const functions on this . 第二常量的意思是“时,可以使用this是一个指针const ”时 ,此功能不能改变mutable 成员变量this并不能调用 const函数this

There, fixed that for you. 那里,为你修好了。

Now it should be clear what a volatile member function means: it can be called when this is a pointer to volatile. 现在应该清楚volatile成员函数的含义:当this是指向volatile的指针时可以调用它。 The requirement that it can only call other member functions if they also are volatile is a consequence, but not the core meaning. 如果它们也是不稳定的,它只能调用其他成员函数的要求是结果,但不是核心含义。

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

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