title: Help with templatizing byteswapping function, performance hit?
body: <pre><code>template&lt;int size&gt;
  inline void* byteswap(void* __x);
  
  template&lt;&gt;
  inline void* byteswap&lt;2&gt;(void* __x)
  {
      return (*(uint16*)__x &gt;&gt; 8) | (*(uint16*)__x &lt;&lt; 8);
  }
  
  template&lt;&gt;
  inline void* byteswap&lt;4&gt;(void* __x)
  {
      return (byteswap&lt;4&gt;(__x &amp; 0xffff) &lt;&lt; 16) | (bswap_16 (__x &gt;&gt; 16));
  }
  
  template&lt;typename T&gt;
  inline T byteswap(T&amp; swapIt)
  {
      return (T*)byteswap&lt;sizeof(T)&gt;(swapIt);
  }    
  
  int main() {
      uint32 i32 = 0x01020304;
      uint16 i16 = 0x0102;
  
      byteswap(i32);
      byteswap(i16);
  
      return 0;
  }
  </code></pre>
  
  <p>The above obviously doesn't even compile.  I'm confused as it seems that I need void* as parameter for the function and things kinda get ugly in byteswap&lt;4> when I need to call byteswap&lt;2> but with a reference.</p>
  
  <p>Any idea how to make this look pretty?  Is it possible for it to achieve (using inlining or other tricks) to make it as performance as doing the bit-operations directly?</p>