title: Dynamically selecting the comparison functor to be used in std::map
body: <p>I have a set of data which needs to be sorted by a criteria that is decided by the user in runtime.</p>
  
  <p>Ideally this sorting criteria has to be passed as a parameter in a function, such as:</p>
  
  <pre><code>void mainFunction(
      InputData const &amp;inputData,
      OutputData &amp;outputData,
      BaseSortingCriteria const &amp;compareFunctor)
  {
  
      /* The data is sorted by this map using the custom provided functor as criteria */
      std::map&lt;InputDataType, ValueType, BaseSortingCriteria&gt; sortedSets(compareFunctor);
  
      ...
  }
  </code></pre>
  
  <p>To get this, I've created a virtual functor representing the base criteria, such as:</p>
  
  <pre><code>struct VirtualSortingCriteria
  {
      virtual bool operator()(
          const InputDataType &amp;var1,
          const InputDataType &amp;var2) const = 0;
  }
  </code></pre>
  
  <p>And to keep a common interface, a base functor, which simply executes a "real" functor passed during construction: </p>
  
  <pre><code>struct BaseSortingCriteria
  {
      BaseSortingCriteria(
          std::shared_ptr&lt;const VirtualSortingCriteria&gt; pCompareFunctor) :
          m_realCompareFunctor(pCompareFunctor)
      {
      }
  
      bool operator()(
          const InputDataType &amp;var1,
          const InputDataType &amp;var2)
      {
          return m_realCompareFunctor-&gt;operator()(var1, var2);
      }
  
  private:
      /** Pointer to the real functor to be used. */
      std::shared_ptr&lt;const VirtualSortingCriteria&gt; const m_realCompareFunctor;
  }
  </code></pre>
  
  <p>And I've defined a couple of "real" functors to test:</p>
  
  <pre><code>struct RealFunctorVersionA final : public VirtualSortingCriteria
  {
      bool operator () (
          InputDataType const &amp;var1,
          InputDataType const &amp;var2) const;
  };
  
  struct RealFunctorVersionB final : public VirtualSortingCriteria
  {
      bool operator () (
          InputDataType const &amp;var1,
          InputDataType const &amp;var2) const;
  };
  </code></pre>
  
  <p>The code that actually uses these different sorting criterias looks like this:</p>
  
  <pre><code>std::shared_ptr&lt;VirtualSortingCriteria&gt; cmpFunctor;
  switch(userSelectedSortingCriteria)
  {
      case CRITERIA_A:
          cmpFunctor.reset(new RealFunctorVersionA);
          break;
      case CRITERIA_B:
          cmpFunctor.reset(new RealFunctorVersionB);
          break;
      default:
          break;
  }
  
  BaseSortingCriteria baseCmpFunctor(cmpFunctor);
  mainFunction(inputData, outputData, baseCmpFunctor);
  </code></pre>
  
  <p>All of this works fine. However, I think it's too complicated to achieve what I want, plus I have the feeling that having to use the polymorphism feature implies the "real" functors can't be inlined anymore, resulting in a probably (although I haven't measured yet) performance penalty.</p>
  
  <p>I'm not sure this could be fixed in a simpler manner using templates (since the functor selection is done at runtime).</p>
  
  <p>Any suggestions? Or am I just overthinking the issue and this is an acceptable solution? What about performance?</p>
  
  <p>I don't use plain C-style functions <code>(bool (*)(InputDataType const &amp;var1, InputDataType const &amp;var2))</code> in the <code>mainFunction()</code> interface to be able to access the extra functionality provided by functors, such as state, construction parameters, etc.</p>
  
  <p>Thanks a lot in advance for your advice.</p>