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

  • Home
  • SEARCH
  • 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 7731683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:32:39+00:00 2026-06-01T06:32:39+00:00

Like many before me, I’m trying so get my derived types to automatically register

  • 0

Like many before me, I’m trying so get my derived types to automatically register with my factory. I read through many question and tried to focus on what I didn’t find there.

I’ve got everything running nicely except the automatic registration.

My Goals:

  1. automatically register any derived class of my base class Base
    1. only classes I mark as registrable
    2. not only direct sub-classes of Base
      • ex: Base -> Device -> Camera -> Webcam
      • this would make using the CRTP like described in this question dificult
  2. minimal changes to the classes I want registered – dummies proof
  3. would prefer using a registrator class than macros
    • like in this question, but I’m not sure if this is dependent on CRTP

What I have:

template <class T>
class abstract_factory
{
    public:
        template < typename Tsub > static void register_class();
        static T* create( const std::string& name );
    private:
        // allocator<T> is a helper class to create a pointer of correct type
        static std::map<std::string, boost::shared_ptr<allocator<T> > > s_map;
};
  • templated abstract factory, with std::string as key type
  • abstract factory has all members and methods static
  • class name is recovered automatically with typeid (no need for text name when registering)
  • registration by calling: abstract_factory<Base>::register_class<MyDerived>();

What I tried (or would like to but don’t know how to properly):

  • registrator<Derived> class: templateded class that is instantiated statically in Derived.cpp and should call abstract_factory::register_class<Derived>() in it’s constructor
    • never gets called or instantiated
    • if I make an instance of Derived in main() this works -> kinda defeats the purpose though
  • declare a simple static variable in each Derived.hpp and set it with the static registration method in Derived.cpp -> again, never gets called.
  • make abstract_factory a true singleton instead of having everything static?

Could use any advice, large or small, thanx.

  • 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-01T06:32:40+00:00Added an answer on June 1, 2026 at 6:32 am

    I use a singleton with a member for registration, basically:

    template< typename KeyType, typename ProductCreatorType >
    class Factory
    {
        typedef boost::unordered_map< KeyType, ProductCreatorType > CreatorMap;
        ...
    };
    

    Using Loki I then have something along these lines:

     typedef Loki::SingletonHolder< Factory< StringHash, boost::function< boost::shared_ptr< SomeBase >( const SomeSource& ) > >, Loki::CreateStatic > SomeFactory;
    

    Registration is usually done using a macro such as:

    #define REGISTER_SOME_FACTORY( type ) static bool BOOST_PP_CAT( type, __regged ) = SomeFactory::Instance().RegisterCreator( BOOST_PP_STRINGIZE( type ), boost::bind( &boost::make_shared< type >, _1 ) );
    

    This setup has a number of advantages:

    • Works with for example boost::shared_ptr<>.
    • Does not require maintaining a huge file for all the registration needs.
    • Is very flexible with the creator, anything goes pretty much.
    • The macro covers the most common use case, while leaving the door open for alternatives.

    Invoking the macro in the .cpp file is then enough to get the type registered at start up during static initialization. This works dandy save for when the type registration is a part of a static library, in which case it won’t be included in your binary. The only solutions which compiles the registration as a part of the library which I’ve seen work is to have one huge file that does the registration explicitly as a part of some sort of initialization routine. Instead what I do nowadays is to have a client folder with my lib which the user includes as a part of the binary build.

    From your list of requirements I believe this satisfies everything save for using a registrator class.

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

Sidebar

Related Questions

Sorry I know similar question have been asked many times before but like most
I know this question has been asked many times before but I tried out
I, like so many programmers before me, am tearing my hair out writing the
This question has been asked many times before but none of the previous ones
I know this is a question that has been asked many times before, but
I have tried what seems like everything - I've done similiar things many times
I know that this question has been asked many times before in different guises
It seems like this title has been used many times before, unfortunately I don't
I know this question has been asked so many times before but I couldn't
So like many developers before me, I am at the crossroads of choosing an

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.