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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:38:28+00:00 2026-05-10T23:38:28+00:00

Suppose I have some per-class data: (AandB.h) class A { public: static Persister* getPersister();

  • 0

Suppose I have some per-class data: (AandB.h)

class A { public:    static Persister* getPersister(); }  class B { public:    static Persister* getPersister(); } 

… and lots and lots more classes. And I want to do something like:

persistenceSystem::registerPersistableType( A::getPersister() ); persistenceSystem::registerPersistableType( B::getPersister() ); ... persistenceSystem::registerPersistableType( Z::getPersister() ); 

… for each class.

My question is: is there a way to automate building a list of per-type data so that I don’t have to enumerate each type in a big chunk (as in the above example)?

For example, one way you might do this is: (AutoRegister.h)

struct AutoRegisterBase {    virtual ~AutoRegisterBase() {}    virtual void registerPersist() = 0;    static AutoRegisterBase*& getHead()    {       static AutoRegisterBase* head= NULL;       return head;    }     AutoRegisterBase* next; };  template <typename T> struct AutoRegister : public AutoRegisterBase {    AutoRegister() { next = getHead(); getHead() = this; }     virtual void registerPersist()    {        persistenceSystem::registerPersistableType( T::getPersister() );    } }; 

and use this as follows: (AandB.cxx: )

static AutoRegister<A> auto_a; static AutoRegister<B> auto_b; 

Now, after my program starts, I can safely do: (main.cxx)

int main( int, char ** ) {     AutoRegisterBase* p = getHead();     while ( p )     {         p->registerPersist();         p = p->next;     }     ... } 

to collect each piece of per-type data and register them all in a big list somewhere for devious later uses.

The problem with this approach is that requires me to add an AutoRegister object somewhere per type. (i.e. its not very automatic and is easy to forget to do). And what about template classes? What I’d really like is for the instantiation of a template class to somehow cause that class to get automatically registered in the list. If I could do this I would avoid having to have the user of the class (rather than the author) to remember to create a:

static AutoRegister< SomeClass<X1> > auto_X1; static AutoRegister< SomeClass<X2> > auto_X2; ... etc.... 

for each template class instantiation.

For FIW, I suspect there’s no solution to this.

  • 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. 2026-05-10T23:38:29+00:00Added an answer on May 10, 2026 at 11:38 pm

    You can execute something before main once if a instantiation of a template is made. The trick is to put a static data member into a class template, and reference that from outside. The side effect that static data member triggers can be used to call the register function:

    template<typename D> struct automatic_register { private:     struct exec_register {         exec_register() {             persistenceSystem::registerPersistableType(                 D::getPersister()             );         }     };     // will force instantiation of definition of static member     template<exec_register&> struct ref_it { };      static exec_register register_object;     static ref_it<register_object> referrer; };  template<typename D> typename automatic_register<D>::exec_register      automatic_register<D>::register_object; 

    Derive the class you want to be auto-registered from automatic_register<YourClass> . The register function will be called before main, when the declaration of referrer is instantiated (which happens when that class is derived from, which will implicitly instantiate that class from the template).

    Having some test program (instead of the register function, a function do_it is called):

    struct foo : automatic_register<foo> {         static void do_it() {         std::cout << ' doit ';      }  };   int main() {      std::cout << ' main ';  } 

    Yields this output (as expected):

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

Sidebar

Ask A Question

Stats

  • Questions 106k
  • Answers 106k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It's not programatically, but you can use CLR Profiler from… May 11, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer If you really want to delve into web application development… May 11, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer In your C# app you generate the byte[] arrays in… May 11, 2026 at 8:59 pm

Related Questions

Suppose I need to have a class which wraps a priority queue of other
I'm new to hibernate, as you'll soon see. I apologize if this question has
My code is built to multiple .dll files, and I have a template class
I am not well acquainted to the compiler magic. The act of transforming human-readable

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.