title: C++ Can constant class data be optimized out of class by compiler?
body: <p>I know that constant variables outside classes can be optimized directly into function calls by the compiler, but is it legal for the compiler to do the same for constant class variables?</p>
  
  <p>If there is a class declared like this:</p>
  
  <pre><code>class A {
  public:
  const int constVar;
      //other, modifiable variables
  
  A(int val): constVar(val) {
           //code to initialize modifiable variables
  
  }
  };
  </code></pre>
  
  <p>and I create an instance of A and call a function like this:</p>
  
  <pre><code>A obj(-2);
  int absoluteVal = std::abs(A.constVar);
  </code></pre>
  
  <p>is the compiler allowed to do this instead and make the class be <code>sizeof(int)</code> smaller?:</p>
  
  <pre><code>A obj();
  int absoluteVal = std::abs(-2);
  </code></pre>