title: Optimizing storage space for classes in C++ in an Arduino library
body: <p>I'm writing an Arduino library to wrap the pin functions (<code>digitalRead</code>, <code>digitalWrite</code>, <code>analogRead</code>, etc.). For example, I have a RegularPin class which is a passthrough and an InvertedPin class which inverts the pin logic. This is useful when going from the breadbord with LEDs to a relay board which inverts the circuit logic. I just have to swap classes.
  I also have a DebouncedPin class for buttons which checks the user presses or releases long enough for the button to be really pressed/released.</p>
  <p>Example for analog pins:</p>
  <pre><code>// AnalogInPin ------------------------------
  class AnalogInPin
  {
    public:
      virtual int read()=0;
      virtual int getNo()=0;
  };
  
  // AnalogRegInPin ---------------------------
  
  template&lt;int pinNo&gt;
  class AnalogRegInPin : public AnalogInPin
  {
    public:
      AnalogRegInPin();
      int read();
      int getNo(){return pinNo;}
  };
  
  template&lt;int pinNo&gt;
  int AnalogRegInPin&lt;pinNo&gt;::read()
  {
      return analogRead(pinNo);
  }
  
  template&lt;int pinNo&gt;
  AnalogRegInPin&lt;pinNo&gt;::AnalogRegInPin()
  {
      pinMode(pinNo, INPUT);
  }
  </code></pre>
  <p>As you can see, I put the pin number in the template declaration because it is not to be changed at run time and I do not want the pin number to use memory when I allocate a pin object, just like in vanilla arduino C code. I know classes can not be of size  zero but read on. Next, I want to write an &quot;AveragedPin&quot; class which will automatically read the selected pin several times and I would like to stack my templated classes like this :</p>
  <pre><code>AveragedPin&lt;cAnalogRegInPin&lt;A0&gt;, UPDATE_ON_READ|RESET_ON_READ&gt; ava0;
  </code></pre>
  <p>or even :</p>
  <pre><code>RangeCorrectedPin&lt;AveragedPin&lt;cAnalogRegInPin&lt;A0&gt;, 
      UPDATE_ON_READ|RESET_ON_READ,RAW_MIN,RAW_MAX,TARGET_RANGE&gt; rcava0;
  </code></pre>
  <p>For the time being, I declared the nested pin as a private member because it is not allowed to use a class object in the template declaration. But then, each layer of nesting uselessly eats several bytes on the stack.</p>
  <p>I know I could use references in the template declaration, but I don't quite understand how that works/should be used. My problem looks like empty member optimization, but it doesn't seem to apply here.</p>
  <p>I feel this is more a C++ question than an arduino one and I'm not a C++ expert. I guess this touches the more advanced parts of C++. Maybe what I want is not possible, or only with recent C++ (20?) revisions.</p>
  <p>Below is the code for the FixedRangeCorrectedPin class.</p>
  <pre><code>template &lt;class P, int rawMin, int rawMax, int targetRange&gt;
  class FixedRangeCorrectedPin : public AnalogInPin
  {
    public:
      int read();
      int getNo(){return pin.getNo();}
    private:
      P pin;
  };
  
  template &lt;class P, int rawMin, int rawMax, int targetRange&gt;
  int FixedRangeCorrectedPin&lt;P, rawMin, rawMax, targetRange&gt;::read()
  {
      int rawRange = rawMax - rawMin;
      long int result = pin.read() - rawMin;
      if (result &lt; 0) result = 0;
      result = result * targetRange / rawRange;
      if (result &gt; targetRange) result = targetRange;
      return result;
  }
  </code></pre>
  <p>My problem is that I would like to remove the 'P pin' class member and replace it in the template declaration like in <code>template &lt;AnalogInPin pin,int rawMin,int rawMax,int targetRange&gt;</code> because which pin is involved here is completely known at compile time.</p>