------- Comment #9 from gdr at gcc dot gnu dot org 2006-01-22 10:44 ------- (In reply to comment #0) > Sometimes a variable is created only for the side-effects of its > constructor/destructor. For example, in the following code: > > struct Closer { ~Closer() { CloseClipboard(); } } closer; > > the closer variable is only used to make sure the clipboard is closed when the > function leaves (normally or exceptionally). > > G++ apparently does not think this is valid use of a variable, and warns: > "warning: unused variable". > > Release: > gcc 3.2 > > How-To-Repeat: > Compile: > > void f () { struct atend { ~atend () { std::cout << "leaving f\n"; } } a; } > > with -Wall. >
The construct reported in this PR is a widely known and used C++ idiom: Resource Acquisition Is Initialization (RAII). It is inappropriate for -Wall to warn about it. However, the mechanism underpining it is so broad that we would need more accurate measure to fix it. Of course, the compiler (more precisely the middle-end) knows that the destructor has a side-effect. But GCC middle-end tends to have little knowledge of language specific idioms. Notice also that a is "used" from a very high level abstraction point of view -- not from byte-fiddling point of view. This is another instance of diagnostic PR better handed off to the front-end. -- Gaby -- gdr at gcc dot gnu dot org changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |gdr at gcc dot gnu dot org http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416