I have this code:
// initializer lists
#include <iostream>
#include <vector>
int main()
{
int values[] { 1, 2, 3 };
std::vector<int> v { 4, 5, 6 };
std::vector<std::string> cities {
"London", "New York", "Paris", "Tokio"
};
return 0;
}
However the gcc
compiler gives me unused variable
warning only for values
array. Why v
and cities
is not reported?
Solution: If variable <variable_name> or function <function_name> is not used, it can be removed. If it is only used sometimes, you can use __attribute__((unused)) . This attribute suppresses these warnings.
Unused variable warnings are emitted during compiletime and should be a hint to you as the developer, that you might have forgotten to actually process a value that you have bound to a name. Thats the main reason for the warning.
No nothing is wrong the compiler just warns you that you declared a variable and you are not using it. It is just a warning not an error. While nothing is wrong, You must avoid declaring variables that you do not need because they just occupy memory and add to the overhead when they are not needed in the first place.
It is not a primitive value, so its constructor and/or destructor might have desired side effects.
Classical example: a Timer object which measures the time between its construction and destruction: https://stackoverflow.com/a/5302868/1938163
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With