5

A lot of people are saying "volatile member function is completely analogous to how const works."

They are quite similar in the sense of if a pointer is marked as const/volatile, it can only access member functions marked as const/volatile.

But actually defining a member function as const has an additional effect, which makes the function read-only. Any modifications of the object inside the function will cause a compiler error. Is there such analogs in volatile member function?

Alex
  • 2,915
  • 5
  • 28
  • 38
  • This link may be of some help. http://stackoverflow.com/questions/5106196/c-what-does-volatile-represent-when-applied-to-a-method – Abhishek Bansal Dec 06 '13 at 08:57

3 Answers3

3

Well, a volatile member function will make the object members volatile, that is, this will be as if it were defined volatile T * const this. And as a consequence, any reference to a member variable is also volatile.

Remember that volatile read/writes are operations that cannot be elided/reordered by the compiler. They are usually used to implement memory-mapped hardware devices or things like that.

Frankly speaking I've never been a use of this feature, other than doing smart tricks to filter the access to the function, not to make use of the volatile-ness of the object. If your code is low level enough to need volatile you probably will want to go putting the volatile just in the variables you need.

rodrigo
  • 94,151
  • 12
  • 143
  • 190
  • Note: whilst the operations on a `volatile` variable cannot be reordered by a compiler, the lack of memory barrier means that an out-of-order processor might reorder them in its execution pipeline and that another processor accessing the same data might not see the changes in the order they are done (TL;DR: this is really hardware specific and does not help with multi-threading). – Matthieu M. Dec 06 '13 at 09:52
  • @MatthieuM.: right, that's why I intentionally did not talk about threads or shared memory. Anyway there are plenty of questions in SO about the semantics of volatile variables, not so many about the meaning of volatile member functions ;-). – rodrigo Dec 06 '13 at 10:21
0

Short answer: yes.

If an instance of an object is declared volatile then it is an error to call non-volatile methods on it (or for those methods to call other non-volatile methods).

A non-volatile instance can still call volatile methods, but not that it is perfectly legal to have two otherwise identical methods in a class - one volatile and one not. In that case a non-volatile instance will call the non-volatile version of the method.

Oliver Matthews
  • 7,497
  • 3
  • 33
  • 36
0

But actually defining a member function as const has an additional effect, which makes the function read-only.

That's a bit of a misconception. It doesn't make the member function read-only - it makes *this const. There's a small, but important, difference between the two (the member function can still modify mutable members, and if it wants to be nasty, it can cast away the const-ness of *this to modify anything it wants, and the compiler won't complain).

And a volatile member function works in the exact same way - it makes *this volatile.

Sander De Dycker
  • 16,053
  • 1
  • 35
  • 40
  • "it prevents the member function from being called for a non-const this" But I am getting compiler errors just by defining the const method. Any modifications of the object inside the function will cause a compiler error. Is not that saying the compiler considers the function as "read-only"? – Alex Dec 06 '13 at 09:04
  • @Alex : the compiler errors are because the `this` is `const` (which is a prerequisite for calling the member function). However, the member function can still modify `mutable` members, and if it wants to be nasty, it can cast away the `const`-ness of `this` to modify anything it wants, and the compiler won't complain. – Sander De Dycker Dec 06 '13 at 09:12
  • "it prevents the member function from being called for a non-const this". This is backwards. If method is *not* const, it can't be called for const this. Other way works of course. – hyde Dec 06 '13 at 09:23
  • @hyde : oh my - mustn't have been awake yet. – Sander De Dycker Dec 06 '13 at 09:45