Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

class classA
{
public:
    const int Foo() 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.

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;

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!

like image 275
milesma Avatar asked Oct 04 '11 05:10

milesma


People also ask

What is the use of volatile keyword in C?

The volatile keyword is intended to prevent the compiler from applying any optimizations on objects that can change in ways that cannot be determined by the compiler. Objects declared as volatile are omitted from optimization because their values can be changed by code outside the scope of current code at any time.

What is volatile variable in C where it is used?

A volatile keyword in C is nothing but a qualifier that is used by the programmer when they declare a variable in source code. It is used to inform the compiler that the variable value can be changed any time without any task given by the source code. Volatile is usually applied to a variable when we are declaring it.

What is volatile keyword and where we can use?

Volatile keyword is used to modify the value of a variable by different threads. It is also used to make classes thread safe. It means that multiple threads can use a method and instance of the classes at the same time without any problem. The volatile keyword can be used either with primitive type or objects.

What do you think is the use of volatile keywords in C++?

The volatile qualifier is applied to a variable when we declare it. It is used to tell the compiler, that the value may change at any time.


2 Answers

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!".

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

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;
};

Hope it helps.

like image 52
Johannes Schaub - litb Avatar answered Oct 27 '22 01:10

Johannes Schaub - litb


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.

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. The requirement that it can only call other member functions if they also are volatile is a consequence, but not the core meaning.

like image 20
Ben Voigt Avatar answered Oct 26 '22 23:10

Ben Voigt