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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:47:09+00:00 2026-06-16T03:47:09+00:00

Automatic class registration in C++ is a common task, and a commonly asked question

  • 0

Automatic class registration in C++ is a common task, and a commonly asked question here on StackOverflow:

Register an object creator in object factory

Somehow register my classes in a list

automatic registration of object creator function with a macro

c++ automatic factory registration of derived types

The basic objective is to register classes automatically with some registry or factory so that it can do some work with each class later.

This is a well-established technique, used by libraries like (for example) Google Test (http://code.google.com/p/googletest), which automatically registers subclasses of the Test class so that each test can be instantiated automatically and run during test execution.

Registration can be accomplished by instantiating a static Registrar class whose constructor does the registration, or by clever use of the CRTP and putting the registration code in the base class constructor, or whatever you like (the links above offer several different possible techniques).

However, when I implement any of these techniques I find they scale very poorly. If I have 10,000 TEST macro invocations in Google Test, compilation and linking grind to a halt (MSVC 2010) and binary size explodes. If I implement this another way, using 10,000 subclasses with static registrars, I see the same behavior.

For instance, consider the simplified example:

#include <iostream>
#include <string>

class Base {

    public:

        Base( const std::string& Name_ ) : Name( Name_ ) { ; }
       ~Base() { ; }

        virtual std::string GetName() const { return Name; }
        virtual void DoSomething() = 0;

    private:

        std::string Name;

};

class Registry {

    public:

        static Registry& GetInstance() {
            static Registry* Instance = new Registry();
            return *Instance;
        }

        void Register( const Base* b ) {
            std::cout << "Registered class " << b->GetName() << std::endl;
        }

    private:

        Registry()  { ; }
       ~Registry()  { ; }

};

class Registrar {

    public:

        Registrar( const Base* b ) {
            Registry::GetInstance().Register( b );
        }

       ~Registrar() { }

};


#define  REGISTER( CLASS )                                          \
    class CLASS : public Base {                                     \
        public:                                                     \
            CLASS( const std::string& Name ) : Base( Name ) { ; }   \
            virtual void DoSomething();                             \
        private:                                                    \
            static Registrar m_Registrar;                           \
    };                                                              \
    Registrar CLASS::m_Registrar( new CLASS( #CLASS ) );            \
    void CLASS::DoSomething()


int main( int argc, char** argv )
{
    return 0;
}


REGISTER( Class1 )
{
    std::cout << "Doing something in Class1" << std::endl;
}

REGISTER( Class2 )
{
    std::cout << "Doing something in Class2" << std::endl;
}

[...]

with a total of 10000 autogenerated REGISTER calls.

Is there a fundamental reason why this won’t scale well? Will the compiler just choke on 10000 classes? Under MSVC 2010 compilation of the above takes almost two minutes on a fairly fast machine and produces a binary over 5 MB in size. If I do similar with Google Test I see the same result.

  • 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-16T03:47:11+00:00Added an answer on June 16, 2026 at 3:47 am

    Writing Java code in C++ rarely works well. All those heap allocations are probably what’s killing performance (as they would in Java, but Java startup is so slow that nobody would notice). Use static objects, and don’t put a Registrar object into each generated class; that’s just a waste of time and space.

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

Sidebar

Related Questions

I know it supports automatic variables, but what about class variables?
Like here: Automatic authentication for Android WiFi Direct I want to create a mobile
I have designed a class with sqlClient.SqlCommand wrappers to implement such functionality as automatic
Base class is Task. There are several derived classes such as PhoneCall, Fax, Email....
Apparently, since Android SDK 17, builds generate an automatic class called BuildConfig and add
I have a class that uses automatic reference counting. Within the class, I have
Like the title says, I'm looking for the options for devises automatic class authenticate_user!
I am using an automatic generated Java class for executing a special method. Because
I'm working on an automatic summarization system in my C++ class and have a
I am trying to write an utility class that permits automatic resizing of images

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.