c++ - 关于隐式取消引用的 GCC 警告

标签 c++ gcc gcc-warning

我刚刚在 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.

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

关于c++ - 关于隐式取消引用的 GCC 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13869318/

相关文章:

c++ - 在 C 中读取 JPEG 文件的 RGB 三元组

c++ - 为什么没有为所有变量报告 "unused variable"警告?

c++ - 使用 Eigen 的插件在第二次运行时崩溃

c - 我如何在 (GNU) C 中编写一个代理函数来连接两个不同的调用约定?

c - 如何使gcc 4.7警告使用臭名昭著的gets()函数?

我们可以混合使用 __extension__ 和 -std=c99 吗?

c++ - 远景——可能有一个带有访问非静态成员的 lambda 的静态类成员吗?

c++ - 调试时自动禁用 Visual Studio 断点

xcode - GCC 警告 "incompatible implicit declaration of built-in function ‘printf’“在 Mac OS X Snow Leopard 下

c++ - 使用 MXE gcc 构建 Qt 应用程序时缺少 std::mutex