GCC关于隐式解除引用的警告 [英] GCC warning about implicit dereference

查看:506
本文介绍了GCC关于隐式解除引用的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 警告:隐式取消引用将不会访问'volatile util'类型的对象在编译这段代码的时候:'在语句[默认启用] 

中:

  volatile util :: Yield y1; 
util ::收益y2;
y1 + = y2; //< ---这里触发了警告。

不幸的是我不太明白GCC试图告诉我什么......



类别Yield的声明如下:

  class Yield {
public:
Yield();

Yield& operator + =(Yield const& other);
收益&运算符+ =(收益率常量&其他);
收益率volatile& operator + =(Yield const& other)volatile;
收益率volatile& operator + =(收益率常量volatile& other)volatile;

//其他操作符剪切...
};

有什么想法?

谢谢! / p>

解决方案

从GCC手册第6.1节 - 何时访问易失性对象?


当使用对volatile进行引用时,G ++不会将等效表达式视为对volatile进行访问,而是会发出警告,指出没有volatile被访问。其原理是否则很难确定发生易失性访问的位置,并且不可能忽略来自返回volatile引用的函数的返回值。再次强调,如果您想强制读取,请将该引用强制转换为右值。

警告源于事实+ =运算符将一个引用返回给一个易失性对象,并且表达式'y1 + = y2'将忽略该返回值。编译器让你知道引用实际上不会被解引用(即不会读取volatile值)。

I just ran across the following warning in GCC:

warning: implicit dereference will not access object of type ‘volatile util::Yield’ in statement [enabled by default]

while compiling this code:

volatile util::Yield y1;
util::Yield y2;
y1 += y2; // <--- Warning triggered here.

and unfortunately I do not quite understand what GCC is trying to tell me...

The class Yield is declared as follows:

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

Any ideas?

Thanks!

解决方案

From the GCC manual, 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.

The warning stems from the fact that the += operator returns a reference to a volatile object, and that the expression 'y1 += y2' ignores that return value. The compiler is letting you know that the reference will not actually be dereferenced (i.e. the volatile value will not be read).

这篇关于GCC关于隐式解除引用的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆