GCC警告有关隐式取消引用(GCC warning about implicit dereferen

2019-07-05 06:29发布

我只是碰到在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是想告诉我...

类产量声明如下:

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

有任何想法吗?

谢谢!

Answer 1:

从GCC手册, 6.1节-当是一种挥发性对象进行访问?

当使用挥发性的引用,G ++不把相当于表达式作为访问挥发物,而是发出不挥发性被访问的警告。 这样做的理由是,挥发性访问发生在哪里,否则就变得难以确定,并且无法忽略返回挥发性引用函数的返回值。 同样,如果你想强行读取,施放参考右值。

警告从+ =运算符返回到易失性对象的引用的事实茎,并且该表达“Y1 + = Y2”忽略返回值。 编译器是让你知道的参考实际上不会解除引用(即挥发性值将不会被读取)。



文章来源: GCC warning about implicit dereference