8

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?

user2864740
  • 60,010
  • 15
  • 145
  • 220
Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
  • because they are not a POD? – Karoly Horvath Mar 01 '14 at 09:58
  • 1
    Those two are class instances and class constructors/destructors might have side effects, e.g. alter some global state. – Hristo Iliev Mar 01 '14 at 09:58
  • 2
    The latter two variables *are* used (in their own construction with potential side effects therein). The trivial POD-types undergoes no such functional construction. The identical usage warning will happen for your others if you change their types to POD-types and, for example, initialize them dynamically. `std::vector *pv = new std::vector{ 4,5,6 };` will issue the identical warning. – WhozCraig Mar 01 '14 at 09:59

1 Answers1

3

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

Community
  • 1
  • 1
Marco A.
  • 43,032
  • 26
  • 132
  • 246