Note that there are some explanatory texts on larger screens.

plurals
  1. POPartially specialize template member function
    text
    copied!<p>I have a template class which looks like the following:</p> <pre><code>template &lt;template &lt;class TypeT&gt; class PoolT=pool_base&gt; struct pool_map { public: template &lt;typename U&gt; struct pool { typedef PoolT&lt;U&gt; type }; public: template &lt;typename T, size_t S=sizeof(T)&gt; T&amp; get( size_t index ); private: pool&lt;uint8_t&gt;::type pool8_; pool&lt;uint16_t&gt;::type pool16_; pool&lt;uint32_t&gt;::type pool32_; pool&lt;uint64_t&gt;::type pool64_; }; template &lt;template &lt;class TypeT&gt; class PoolT&gt; template &lt;typename T, size_t S&gt; inline T&amp; pool_map&lt;PoolT&gt;::get( size_t index ) { // Default case } template &lt;template &lt;class TypeT&gt; class PoolT&gt; template &lt;typename T&gt; inline T&amp; pool_map&lt;PoolT&gt;::get&lt;T,8&gt;( size_t index ) { // Dispatch to pool8_ } template &lt;template &lt;class TypeT&gt; class PoolT&gt; template &lt;typename T&gt; inline T&amp; pool_map&lt;PoolT&gt;::get&lt;T,16&gt;( size_t index ) { // Dispatch to pool16_ } template &lt;template &lt;class TypeT&gt; class PoolT&gt; template &lt;typename T&gt; inline T&amp; pool_map&lt;PoolT&gt;::get&lt;T,32&gt;( size_t index ) { // Dispatch to pool32_ } </code></pre> <p>You obviously noticed that I wrote what would be possible in a wonderful and ideal world where default template parameters and partial specialization of template methods are possible (without specializing the entire class).</p> <p>I would like to ear about advices to achieve the same effect, that is, being able to have a specialized method for each size S so that I can select the proper pool to use. I tried to add a <code>get_pool&lt;size_t&gt;</code> inside the pool_map, but almost the same problem happens: i can't specialize the inner class without specializing the outer one...</p> <p>The only solution that comes to my mind, would be to use an outer <code>get_pool&lt;size_t&gt;</code> class that would take the pool_map as parameters an returns a reference to the <code>poolX_</code> member, but I would like to avoid it, since it does not seems to be "the real static way".</p> <p>Thanks for reading!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload