This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [Consult] g++: About "-Wunused-variable" for constant variable initialized by function


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]