1

I find it strange that by enabling optimizations some code would "compile" that otherwise wouldn't. Here I mean (by "compile") to go through all stages of compilation and create an executable (i.e. including linking). Consider the following code:

struct A { static constexpr int i{0}; };
int main() {
    const int *p = &A::i;
    return *p;
}

This code is perfectly acceptable to g++ -std=c++14 -O1, but if I pass -O0 the linker complains about missing symbols (as I would anticipate). I can see how an optimizer could tell that it doesn't need those symbols, but it was strange to me that one actually failed while the other succeeded. Is this intentional behavior by the compiler? Is it a "design technique" of some sort? Is it possible to "force" the compiler to not emit these symbols if possible (say, for an embedded environment)?

Any insight would be appreciated.

I'm using gcc 7.3.0

Barry
  • 286,269
  • 29
  • 621
  • 977
Nathan Chappell
  • 2,099
  • 18
  • 21
  • Taking the address of something means you need a definition, and you don't have one. It's just that in some cases, that happens to be able to be optimized away. – Barry Oct 17 '18 at 20:24
  • There might be better duplicate targets. – Barry Oct 17 '18 at 20:26

0 Answers0