title: Type-casting using function call T(0) and templates
body: <p>I have a function converting a "simulation domain" into an array of voxels (3D pixels). Each voxel can be 0 or 1, or at least <code>false</code> or <code>true</code>. 
  As the domain can be really big, as the discretisation step can be very small, it requires a very big amount of memory.</p>
  
  <p>I use my array of voxels to perform scattering simulations using FFTW3 (In that case, it's just great if the function can work on an array of <code>double[2]</code> (complex type known by FFTW3, so that I can perform in-place transform). But when I use my array of voxels to perform "simple" operations (volume fraction estimation for example) I just want my array to be of the smallest type I can work with, so an array of boolean would be just fine.</p>
  
  <p>I was thinking of something like the following  : </p>
  
  <pre><code>#include &lt;iostream&gt;
  int N = 10 ;
  
  
  template&lt;typename T&gt;
  void voxelizator(T* &amp; Voxels) {
      // N = total number of voxels, stored in a row-major 1D array
      if (Voxels==NULL) Voxels = new T[N] ; //
      for (int n = 0 ;  n &lt; N ; n++) Voxels[n] = T(0) ;
      { // Code to voxelizate the space -- DUMB condition 
          for (int n = 0 ; n&lt;N ; n++) if (n%2==0) Voxels[n] = T(1) ;
      }
  }
  
  
  
  int main()
  {
      int * voxOfInt = NULL ;
      std::cout &lt;&lt; "Array of int :" &lt;&lt; std::endl ;
      voxelizator(voxOfInt) ;
      std::cout &lt;&lt; "Done" &lt;&lt; std::endl ;
      for (int n = 0 ; n &lt; N ; n++)  std::cout &lt;&lt; n &lt;&lt; "\t" &lt;&lt; voxOfInt[n] &lt;&lt; std::endl ;
      delete[] voxOfInt ;
  
      bool * voxOfBool = NULL ;
      std::cout &lt;&lt; "Array of boolean " &lt;&lt; std::endl ;
      voxelizator(voxOfBool) ;
      std::cout &lt;&lt; "Done" &lt;&lt; std::endl ;
      for (int n = 0 ; n &lt; N ; n++)  std::cout &lt;&lt; n &lt;&lt; "\t" &lt;&lt; voxOfBool[n] &lt;&lt; std::endl ;
      delete[] voxOfBool ;
  
  }
  </code></pre>
  
  <p>but the error <code>ISO C++ forbids casting to an array type 'double[2]'</code> is fired when i call this function with an array of double[2] (complex from FFTW3) (or int[2] like in this example : )</p>
  
  <pre><code>...    
  typedef double complex [2]  ;
  ...
  // in main {}
  
      complex * voxOfArray = NULL ;
      std::cout &lt;&lt; "Array of an array " &lt;&lt; std::endl ;
      voxelizator(voxOfArray) ; // It fails to call the T(0) at compile time, but i would be more than happy to explain g++ what is T(0) for int[2] ...
      std::cout &lt;&lt; "Done" &lt;&lt; std::endl ;
      for (int n = 0 ; n &lt; N ; n++)  std::cout &lt;&lt; n &lt;&lt; "\t" &lt;&lt; voxOfArray[n] &lt;&lt; std::endl ;
      delete[] voxOfArray ;
  </code></pre>
  
  <p>The question is : Where can I give g++ the recipie to typecast :</p>
  
  <ul>
  <li>T(0) into {0,0} and T(1) into {1,0} when T is double[2] ?</li>
  <li>T(0) into FALSE and T(1) into TRUE when T is a boolean ?</li>
  </ul>
  
  <p>Note : 
  The function call operator for a type seems weird to me but I know it works fine with the following sign function I already use (Please, insert here the picture of the dog doing some chemistry and saying "I've no idea what I'm doing")</p>
  
  <pre><code>template &lt;typename T&gt; int sign(T value) {
      return (T(0) &lt; value) - (value &lt; T(O)) ; 
  }
  </code></pre>