title: C++ generic class design with methods which return subclass
body: <p>I am writing a generalized list for Unicode code-point. The problem is I also want some general class design which allowing specialized version. Here's the example.</p>
  
  <p>This is an example.</p>
  
  <pre><code>class
  MyString
  {
  public:
      auto getItemAt(int index) const -&gt; UnicodePoint const;
      auto subsetInRange(int index, int length) const -&gt; MyString const;
      auto getUTF8Binary() const -&gt; std::string const;
  };
  </code></pre>
  
  <p>Here's only one, but actually, there're bunch of methods like <code>subsetInRange</code>, so I don't want to repeat to write them for other data types. That leads me to write a generic version superclass.</p>
  
  <pre><code>template&lt;typename T, typename FINAL_SELF_TYPE&gt;
  class
  MyList
  {
  public:
      auto getItemAt(int index) const -&gt; T const;
      auto subsetInRange(int index, int length) const -&gt; FINAL_SELF_TYPE const;
      // getUTF8Binary() removed because it's inappropriate on this abstraction level.
  };
  
  class
  MyString : public MyList&lt;UnicodePoint, MyString&gt;
  {
  public:
      auto getUTF8Binary() const -&gt; std::string const;
  };
  </code></pre>
  
  <p>Finally I had to admit I have to pass final class type as a template parameter to <code>MyList</code> class to make it work properly. And it feels something wrong. I can't say what it is exactly, but my brains keep sending warning signal. I think it's because I am putting some specialization information on abstracted general class. Which seems circular dependency - not good at any case. It's fully against to class hierarchy.</p>
  
  <p>But when I write some general classes has methods return same type as a result, I always see this pattern. It's bugging me. Due my short C++ experience, I cannot have some confidence on my design.</p>
  
  <p>How is this pattern to you? Good or wrong? If it's wrong, what's wrong?</p>