我刚刚在海湾合作委员会中发出以下警告:
warning: implicit dereference will not access object of type ‘volatile util::Yield’ in statement [enabled by default]
在编译此代码时:
volatile util::Yield y1; util::Yield y2; y1 += y2; // <--- Warning triggered here.
不幸的是,我不太明白GCC试图告诉我的是什么……
Yield类声明如下:
class Yield { public: Yield(); Yield &operator+=(Yield const &other); Yield &operator+=(Yield const volatile &other); Yield volatile &operator+=(Yield const &other) volatile; Yield volatile &operator+=(Yield const volatile &other) volatile; // Other operators snipped... };
有任何想法吗?
谢谢!
解决方法
来自GCC手册,Section 6.1 – When is a Volatile Object Accessed?
When using a reference to volatile,G++ does not treat equivalent expressions as accesses to volatiles,but instead issues a warning that no volatile is accessed. The rationale for this is that otherwise it becomes difficult to determine where volatile access occur,and not possible to ignore the return value from functions returning volatile references. Again,if you wish to force a read,cast the reference to an rvalue.
该警告源于以下事实:=运算符返回对volatile对象的引用,并且表达式’y1 = y2’忽略该返回值.编译器让你知道引用实际上不会被解引用(即不会读取volatile值).