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

C++ GCC关于隐式取消引用的警告,c++,gcc,gcc-warning,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手册 当使

我刚刚在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手册

当使用对volatile的引用时,G++不会将等效表达式视为对volatile的访问,而是发出警告,表示未访问volatile。这样做的基本原理是,否则很难确定易失性访问发生在何处,也不可能忽略返回易失性引用的函数的返回值。同样,如果要强制读取,请将引用强制转换为右值


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

此警告对于c++11应该是过时的,因为在c++11中,函数调用的左值将不再从中读取。@Johanneschaub litb:已经有几年了,但我在c++14中仍然收到此警告(目前在GCC的最新版本中,它让人沮丧)。我想知道是否有更深层次的需求,或者它只是一个遗物。最近的叮当声并没有给出这样的警告,但从《戈德博尔特》的旧版本来看,似乎从来没有发出过这样的警告。如果你不想强制读取,你如何编写代码来避免警告?@BenVoigt:
void operator=(Yield const&other)volatile;
完全没有返回值不是一个好的解决方案。如果它在某些上下文中使用,而不是在其他上下文中使用,该怎么办?
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...
};