This is the mail archive of the 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 12 October 2014 16:32, Chen Gang <> wrote:
> Hello All:
> I found an issue about g++, it is OK for "-Wall -O0", but will report
> -Wunused-variable for "-Wall -O1|2|3|s". The original version (e.g.
> gcc 4.8.3 redhat version) does not report warning for "-Wall -O?".
> The related operation:
>   [root@localhost qemu_cc]# cat
>   const char n() { return 1; }
>   const char c =  n();
>   [root@localhost qemu_cc]# /usr/local/bin/g++ -Wall -O0 -c -o test.o
>   [root@localhost qemu_cc]# /usr/local/bin/g++ -Wall -O2 -c -o test.o
> warning: 'c' defined but not used [-Wunused-variable]
>    const char c =  n();
>               ^

This cut down test case seems to have discarded two interesting
things about the original issue
 (1) the definitions of the const variables are in a header
 (2) gcc doesn't warn about const declarations in the same
     header which use 'int' rather than 'float' or 'double'

That said, how does g++ know that the variable isn't defined
for the benefit of another translation unit? (Conversely, how
should a library define constants in a header for the benefit
of users of the library in a way that doesn't make g++ complain
if the library using code happens not to use the constant?)

-- PMM

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