title: Policy Based Design C++ Questions
body: <p>I've been reading the book by Andrei Alexandrescu Modern C++ Design. And i have a question about the decomposition of a class into policies. </p>
  
  <p>Basically what would be a good policy size ? Most examples show parts like construction, destruction, thread safety and so on. Simply put small policies :).</p>
  
  <p>What if i want to create a file io class, that takes a file type as a policy, e.g.</p>
  
  <pre><code>struct XX_Type
  {
      void  AsyncRead(callback c);
      void* Write(const uint8* image);
  }
  
  struct YY_Type
  {
      void  AsyncRead(callback c);
      void* Write(const uint8*, image, uint32 offset);
  };
  
  template&lt;class FileType = XX_Type&gt;
  class File : public FileType
  {
      virtual void OnDataRead(const uint8*, uint32 size) = 0;
      ...
  };
  </code></pre>
  
  <p>Idea is to inherit from file and create a different templates that can later be spawned when needed. Would this be a good fit for policies or should i just pass the file handle to global static functions or should i just create a class for each type of file ? I want to make sure the user errors are low :) and policies seem to be less error prone.</p>
  
  <p>Edit:</p>
  
  <p>Thanks @Claudiordgz for a great indept answer</p>
  
  <p>To give another example would be taking the network approach.</p>
  
  <p>UPD and TCP are very similar and very different at the same time. They both need a socket but one is connectionless the other connection-oriented. But logically they still remain a part of the transport and if i would like to create a higher level of abstraction say a application layer protocol, so taking @Claudiordgz it would at least for me make sense to use the transportlayer as a policy due to its place on the network stack.</p>