title: Virtual destructor moves object out of rodata section
body: <p>I have a large number of static constant objects which are constructed with a constexpr constructor, so they are immediately stored in the final binary without any constructor call.</p>
  
  <p>Since I'm working on a low RAM system (STM32 MCU), I want to reduce the memory footprint of these objects and since they are constant, store them in the <code>.rodata</code> section instead. The compiler managed this without a problem.</p>
  
  <p>But, now that I added a virtual destructor to the base class in order to remove compiler warnings, the objects are stored in the <code>.data</code> section instead.</p>
  
  <p>Of course, I could use some <code>#pragma</code> to remove the compiler warnings for the base class specifically and remove the virtual destructor, but I want to know if there is a cleaner solution to this.</p>
  
  <p>Minimalism code showcasing the problem:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>class Object {
      int value;
  public:
      constexpr Object(int param) 
      : value(param) {}
  
      virtual int getValue() const = 0;
  
      virtual ~Object() = default; // This line causes problems
  };
  
  class Derived : public Object {
      volatile int otherValue;
  public:
      constexpr Derived(int param1, int param2) 
      : Object(param1), otherValue(param2) {}
  
      int getValue() const override { return otherValue; }
  };
  
  
  const Derived instance(1,2);
  
  int main() {
      return instance.getValue();
  }
  </code></pre>
  
  <p>Also, here is a CompilerExplorer to compare with and without the virtual destructor: <a href="https://godbolt.org/z/M5G7LO" rel="noreferrer">https://godbolt.org/z/M5G7LO</a></p>