This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
- From: "mutz at kde dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 10 Jun 2004 19:31:27 -0000
- Subject: [Bug c++/10416] 'unused variable' warning ignores ctor/dtor side-effects
- References: <20030415185600.10416.gccbugs@contacts.eelis.net>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From mutz at kde dot org 2004-06-10 19:31 -------
(In reply to comment #1)
> State-Changed-From-To: open->closed
> State-Changed-Why: The compiler cannot know what you want. If you don't like
> the warning, switch it off, or follow what the manual
<snip>
Sorry, but this reasoning doesn't hold.
See e.g. (I'll attach this file in a minute):
---begin broken-warning.cpp---
extern void foo();
class AtTheEndOfMain {
public:
~AtTheEndOfMain() {
foo();
}
};
class AtTheStartOfMain {
public:
AtTheStartOfMain() {
foo();
}
};
int main() {
AtTheEndOfMain ateom;
AtTheStartOfMain atsom;
return 0;
}
---end broken-warning.cpp---
$ g++ -Wall -c -o broken-warning.{o,cpp}
broken-warning.cpp: In function `int main()':
broken-warning.cpp:19: warning: unused variable `AtTheEndOfMain ateom'
What's the difference between dtor side effects (which are ignored) and ctor
side effect (which are honoured w.r.t. the unused-warning)?
The difference is probably that you yourself are so used to using
resource-allocation-is-initialization that you don't want to be warned about
every use of it, so the ctor side effects are honoured. It simply seems that
no-one checked for dtor-only side effects, and although I agree that they're
not very often used in normal C++, they're often used when you do AOP in C++.
This warning is also not at all useful. Warnings are there for the case where
there is potential discrepancy between what the user writes and what he wants.
This warning should only be emitted when the compiler can be sure that removing
the variable has no side-effects. Yes, there are cornercases where the removal
of an automatic variable can trigger a bufferoverflow to become visible etc,
but let's not get hypothetical.
This warning is taken by users to mean "you can remove this variable, it is not
used". And it's dangerous if you can't rely on it. A warning that you can't do
anything about except setting a proprietary attribute is wrong.
Thinking of it, the variable isn't at all unused: you need it to pass it as the
this pointer when invoking the dtor...
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=10416