本文介绍了C ++ - 当应用于方法时,volatile表示什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个C ++方法声明如下:

If I have a C++ method declaration as follows:

class A
{
public:
   double getPrice() volatile;
};




  1. volatile

您可能感兴趣在 Dobbs博士文章由Andrei Alexandrescu。我是:)

You might be interested in this Dr Dobbs article by Andrei Alexandrescu. I was :)

编辑:
这篇文章是写了一段时间,现在看起来社区已经移动。 Herb Sutter有这个说法。感谢Iain(和Herb!)

That article was written a while back and now it seems that the community has moved on. Herb Sutter has this to say this. Thanks Iain (and Herb!)

mlimber 指出Andrei有后续文章,他继续主张使用volatile正确性作为检测支持POSIX样互斥体的系统上的竞争条件的有价值工具。

mlimber points out that Andrei had a follow up article here where he continues to advocate the use of volatile correctness as a valuable tool for detecting race conditions on systems supporting POSIX-like mutexes.

推荐答案

这是一个volatile成员,就像const成员只能在const对象上调用,只能在volatile对象上调用。

It is a volatile member which, just like a const member can only be called on const objects, can only be called on volatile objects.

有什么用处?好吧,全局volatile是没有什么用的(通常被误解为适用于MT编程,在C ++中不是这样,参见例如),而且volatile类对象更没有用。

What's the use? Well, globally volatile is of little use (it is often misunderstood to be applicable for MT programming, it isn't the case in C++, see for instance http://www.drdobbs.com/high-performance-computing/212701484), and volatile class objects are even less useful.

IIRC A. Alexandrescu建议使用对volatile对象进行的类型检查来静态地确保一些属性对MT编程有用(例如,在调用成员函数之前已经进行了锁定)。可悲的是,我没有找到文章。 (这里是:)

IIRC A. Alexandrescu has proposed to use the type checking done on volatile objects to statically ensure some properties usefull for MT programming (say that a lock has been taken before calling a member function). Sadly, I don't find the article back. (Here it is: http://www.drdobbs.com/184403766)

编辑:添加评论中的链接(他们在问题中添加的链接)。

added links from the comments (they where added also in the question).

这篇关于C ++ - 当应用于方法时,volatile表示什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:48