g++ does not show a ‘unused’ warning

It is not a primitive value, so its constructor and/or destructor might have desired side effects.

To illustrate that this happens in practice: I use a class to time sections of code, that looks roughly like this:

class Timed {
    double start;
    public:
        Timed() { start = now(); }
        ~Timed() { std::cout << (now() - start) << '\n'; }
}

So to measure how long a function takes, I simply do:

void slow() {
    Timed t;
    // heavy operation here...
}

The variable t never gets used, but it’s still important to the behaviour of the code.

Leave a Comment

tech