Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1008645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:48:12+00:00 2026-05-16T08:48:12+00:00

I have a template class which looks like the following: template <template <class TypeT>

  • 0

I have a template class which looks like the following:

template <template <class TypeT> class PoolT=pool_base>
struct pool_map
{
public:
  template <typename U> struct pool { typedef PoolT<U> type };

public:
  template <typename T, size_t S=sizeof(T)>
  T& get( size_t index );

private:
  pool<uint8_t>::type  pool8_;
  pool<uint16_t>::type pool16_;
  pool<uint32_t>::type pool32_;
  pool<uint64_t>::type pool64_;
};

template <template <class TypeT> class PoolT>
template <typename T, size_t S>
inline
T& pool_map<PoolT>::get( size_t index )
{
  // Default case
}

template <template <class TypeT> class PoolT>
template <typename T>
inline
T& pool_map<PoolT>::get<T,8>( size_t index )
{
  // Dispatch to pool8_
}

template <template <class TypeT> class PoolT>
template <typename T>
inline
T& pool_map<PoolT>::get<T,16>( size_t index )
{
  // Dispatch to pool16_
}

template <template <class TypeT> class PoolT>
template <typename T>
inline
T& pool_map<PoolT>::get<T,32>( size_t index )
{
  // Dispatch to pool32_
}

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).

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 get_pool<size_t> inside the pool_map, but almost the same problem happens: i can’t specialize the inner class without specializing the outer one…

The only solution that comes to my mind, would be to use an outer get_pool<size_t> class that would take the pool_map as parameters an returns a reference to the poolX_ member, but I would like to avoid it, since it does not seems to be “the real static way”.

Thanks for reading!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:48:12+00:00Added an answer on May 16, 2026 at 8:48 am

    There is one easy solution (maybe you didn’t think about it), which is:

    template <typename T>
    T& pool_map<PoolT>::get( size_t index )
    {
      if (sizeof(T) * CHAR_BIT == 8) {
         // Dispatch to pool8_
      } else if (sizeof(T) * CHAR_BIT == 16) {
         // Dispatch to pool16_
      } else if (...) {
         ...
      } else {
        // Default case
      }
    }
    

    But since this will maybe give you compilation errors (depending on what you put instead of “dispatch to …”) you could simply overload the get() function.

    template <typename T>
    typename std::enable_if<sizeof(T) * CHAR_BIT == 8,T>::type&
        get( size_t index )
    {
       ...
    }
    
    template <typename T>
    typename std::enable_if<sizeof(T) * CHAR_BIT == 16,T>::type&
        get( size_t index )
    {
       ...
    }
    
    etc.
    

    The only problem is that the default implementation (if you need any) needs a condition like sizeof(T) * CHAR_BIT != 8 && sizeof(T) * CHAR_BIT != 16 && sizeof(T) * CHAR_BIT != 32 && sizeof(T) * CHAR_BIT != 64

    The last solution (the best one I’d say) would still to use a private class with a static function which you can specialize (what you said was not the “real static way”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function which looks like this: template <class In, class In2>
I have a template class that I serialize (call it C), for which I
The problem: I have a class which contains a template method execute which calls
I have a template class like below. template<int S> class A { private: char
I have got a template class as follows: class MyClass<T> { T field; public
I have a template class that is only valid for couple of template parameters:
I have a template class where I want to use objects of that class
I have a template class for thread-safe vector: template <class T> class SharedVector {
My code is built to multiple .dll files, and I have a template class
I have a C++ template class that gets instantiated with 3 different type parameters.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.