Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

c++ - GCC warning about implicit dereference

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...