Variable not detected as not used

I am using g ++ 4.3.0 to compile this example:

#include <vector> int main() { std::vector< int > a; int b; } 

If I compile the example with the maximum warning level, I get a warning that the variable b is not used:

 [ vladimir@juniper data_create]$ g++ m.cpp -Wall -Wextra -ansi -pedantic m.cpp: In function 'int main()': m.cpp:7: warning: unused variable 'b' [ vladimir@juniper data_create]$ 

Question: why the variable a is not indicated as not used? What parameters do I need to pass to get a warning for variable a ?

+6
c ++ gcc gcc-warning g ++
source share
3 answers

Theoretically, the default constructor for std::vector<int> can have arbitrary side effects, so the compiler cannot figure out whether deleting the definition of a can change the semantics of the program. You will receive this warning only for built-in types.

The best example is locking:

 { lock a; // ... // do critical stuff // a is never used here // ... // lock is automatically released by a destructor (RAII) } 

Even if a never used after defining it, deleting the first line would be wrong.

+23
source share

a is not inline. In fact, you call the constructor std::vector<int> and assign the result a. The compiler sees this as use because the constructor may have side effects.

+1
source share

A is actually used after it is declared as its destructor, called at the end of its scope.

+1
source share

All Articles