This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: [Consult] g++: About "-Wunused-variable" for constant variable initialized by function
- From: Chen Gang <gang dot chen dot 5i5j at gmail dot com>
- To: Jakub Jelinek <jakub at redhat 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 21:59:03 +0800
- 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> <20141013133153 dot GU10376 at tucnak dot redhat dot com>
On 10/13/14 21:31, Jakub Jelinek wrote:
> 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.
>
OK, thank you for your explanation. Excuse me, I am not quite familiar
with C++. I guess your meaning is:
- "char f(){return 1}; const char a = f();", if 'a' is not used, need
report warning (no matter whether it is in header or source file).
- "const float a = 3.1 + 7.0/2;", if 'a' is not used, need nor report
warning (no matter whether it is in header or source file).
- this warning is based on C++11, so old gcc version may not support
this warning, but new gcc (e.g. 5.0), need support it.
Is it correct?
Thanks.
--
Chen Gang
Open, share, and attitude like air, water, and life which God blessed