This is the mail archive of the
mailing list for the GCC project.
Re: [Consult] g++: About "-Wunused-variable" for constant variable initialized by function
- From: Jakub Jelinek <jakub at redhat dot com>
- To: Chen Gang <gang dot chen dot 5i5j at gmail dot com>
- Cc: Peter Maydell <peter dot maydell at linaro dot org>, Jeff Law <law at redhat dot com>, jason at redhat dot com, nathan at codesourcery dot com, gcc at gcc dot gnu dot org
- Date: Mon, 13 Oct 2014 15:31:53 +0200
- Subject: Re: [Consult] g++: About "-Wunused-variable" for constant variable initialized by function
- Authentication-results: sourceware.org; auth=none
- References: <543A910F dot 7060301 at gmail dot com> <CAFEAcA8__ewvHy83-VX_iASm_=mBRA_jCdy8bMRPHt_H-EeXhw at mail dot gmail dot com> <543BCF47 dot 1040703 at gmail dot com>
- Reply-to: Jakub Jelinek <jakub at redhat dot com>
On Mon, Oct 13, 2014 at 09:10:31PM +0800, Chen Gang wrote:
> Oh, yes. Originally I got this warning by compiling Qemu. And sorry for
> my sample (test.cc) may be not quite precise.
> For me, I guess:
> - If the constant number is defined in the header file, and never be
> used, our g++ need not report warning about [-Wunused-variable].
That is nonsense, even if you define such a "constant" in a header file,
it still means runtime overhead (the variable needs to be constructed at
runtime, const is not the same thing as constexpr).
So, IMHO the warning is desirable even if it is in headers, it is something
you should reconsider. Making the function constexpr makes the warning of
course go away, then there is no runtime overhead associated with it; but
you'll need C++11 for that.