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 8121461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:29:43+00:00 2026-06-06T05:29:43+00:00

Does anyone know of a method to use CRTP to count the number of

  • 0

Does anyone know of a method to use CRTP to count the number of subclasses of an object?

Suppose we had a setup similar to the following one:

template <typename T>    
class Object
{
    ....  
};

const unsigned int ObjectSubClassCount = ...; 

class Subobject : public Object<SubObject>
{
    ....
};

class Second : public Object<Second>
{
    ....
};

and so on, such that, using TMP, we might have a constant (ObjectSubClassCount) that represents the total number of subclasses?

Does anyone know a way to do this?

Edit: I am wanting to use the result as a template parameter later on, so I need it to be done with TMP…

  • 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-06-06T05:29:44+00:00Added an answer on June 6, 2026 at 5:29 am

    Without the requirement to use the result as a template parameter later I would try it doing like this:

    // Class which increments a given counter at instanciation
    struct Increment {
      Increment(std::size_t& counter)
      {
        counter++;
      }
    };
    
    // This is your template base
    template <typename T>    
    class Object
    {
      private:
        // For every instanciation of the template (which is done for a subclass)
        // the class counter should get incremented
        static Increment incrementCounter;
    };
    
    // This is the global object counter
    static std::size_t classCounter;
    
    // Static Member Variable
    template<typename T>
    Object<T>::incrementCounter(classCounter);
    

    Haven’t tried it but should do. To have the result available as a template parameter again (MPL) I don’t have enough experience in MPL but I doubt this is possible.

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

Sidebar

Related Questions

Does anyone know if there's a built in method/object I can use or an
does anyone know if the following java Method in the java.util.concurrent Package ScheduledExecutorService.html#scheduleAtFixedRate() absolutely
I'm getting a Object doesn't support this property or method error, does anyone know
Does anyone know a method to use to get a rough size of an
Does anyone know why I get the following error when I use the web_step#follow
Does anyone know how what Spring Jdbc template method I could use to execute
Does anyone know why I get undefined method `my_method' for #<MyController:0x1043a7410> when I call
Does anyone know of any way to overcome NotSupportedException? I have a method against
Does anyone know of a free tool, similar to what is built into Visual
Does anyone know of any websites, or (preferably) downloadable packages that you can use

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.