Hi,

I've included a small program at the end of my email.

Here's what happens: in callInitA(), an Object put onto the stack (which has a const member variable, initialized to 0). Then somefunction called (which is intentionally not defined). Then ~Object() is called, which has an "if", which has a not-immediately-obvious, but always false condition. Compiling with -03, everything gets inlined.

My question is about the inlined ~Object(). As m_initType is always 0, why does not optimize the destructor GCC away? GCC inserts code that checks the value of m_initType.

Is it because such construct is rare in practice? Or is it hard to do an optimization like that?

I've checked other compilers (clang, icc, msvc), none of them optimized this.

(I've tried gcc 5.1, 6.1, 7.1)

Thanks,
Geza

---- my program -----

#include <cstdlib>

class Object {
private:
    const int m_initType = 0;
public:
    ~Object() {
        if (m_initType == 111) {
            abort();
        }
    }
};

void somefunction(const Object& object);

void callInitA() {
    Object o;
    somefunction(o);
}

Reply via email to