c – GCC关于隐式解除引用的警告

前端之家收集整理的这篇文章主要介绍了c – GCC关于隐式解除引用的警告前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚在海湾合作委员会中发出以下警告:
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值).

原文链接:https://www.f2er.com/c/118180.html

猜你在找的C&C++相关文章