gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 18:37:06 30 4
gpt4 key购买 nike

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

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com