title: Advantages of type traits vs static members?
body: <p>I have a class (<code>Voxel</code>) with subclasses which may or may not have a number of different properties (material, density, etc) with get and set methods. Now, I want to write some code as follows:</p>
  
  <pre><code>template &lt;typename VoxelType&gt;
  void process(VoxelType voxel)
  {
    if(VOXEL_HAS_MATERIAL)
    {
      //Do some work which involves calling get/setMaterial()
    }
    if(VOXEL_HAS_DENSITY)
    {
      //Do some work which involves calling get/setDensity()
    }
  }
  </code></pre>
  
  <p>I would therefore like to implement the <code>VOXEL_HAS_MATERIAL</code> and <code>VOXEL_HAS_DENSITY</code> parts. Two simple options are:</p>
  
  <ol>
  <li>Add static <code>hasMaterial()</code> and <code>hasDensity()</code> methods to the <code>Voxel</code> class, to be overridden in derived classes.</li>
  <li>Create a type traits class with <code>hasMaterial()</code> and <code>hasDensity()</code>, and specialize this for each <code>Voxel</code> subclass.</li>
  </ol>
  
  <p>Using method (2) allows the traits to be defined for primitive types (int, etc) but this is not useful in my  case. Are there any further advantages to using type traits here or should I go for the simpler static method approach?</p>
  
  <p><strong>Note:</strong> I'm also aware of SFINAE based approaches which I shall consider separately.</p>
  
  <p><strong>Edit1:</strong> I've changed the sample code to show the use of templates. I'm looking for static rather than runtime solutions to this problem. Ideally, the compiler would be able to strip out the code in the if statements if it determines they can not be executed for the given type.</p>