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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:01+00:00 2026-06-17T18:28:01+00:00

I have an implementation of a registrar for classes that are derived from a

  • 0

I have an implementation of a registrar for classes that are derived from a certain base class. The intent is to have each derived class register itself and in the process provide some information about itself, which in the example below is provided through a string.

What I’m missing is a way to force classes that derive from Base to register themselves through initializing the static member reg. In other words, is it possible to have the compiler somehow produce an error if a derived class doesn’t define/initiatlize the static member?

struct Registrar {
  Registrar(string type) {
    registry().push_back(type);
  }
  static vector<string> & registry() {
    static vector<string> * derivedTypes = new vector<string>;
    return *derivedTypes;
  }
};

//CRTP
template <typename Derived>
class Base
{
  static Registrar reg;
};

class Derived1 : public Base<Derived1> {/*Class definition*/};
class Derived2 : public Base<Derived2> {/*Class definition*/};
class Derived3 : public Base<Derived3> {/*Class definition*/};
//...

//Initialize the static members of each derived type
//Commenting out any of the following 3 lines doesn't produce an error.
//Ideally, I want it to produce a compile error.
template<> Registrar Base<Derived1>::reg("Derived1");
template<> Registrar Base<Derived2>::reg("Derived2");
template<> Registrar Base<Derived3>::reg("Derived3");

int main() {

  cout << "Registered Types:" << endl;
  for(vector<string>::const_iterator it = Registrar::registry().begin();
      it != Registrar::registry().end(); ++it) {
    cout << *it << endl;
  }

  return 0;
}
  • 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-17T18:28:03+00:00Added an answer on June 17, 2026 at 6:28 pm

    You can cause a linker error in g++ at least by adding a dummy function to the registrar: void blah() { } and then calling it from a dummy constructor in the CRTP base class:

    public:
        Base() { reg.blah(); }
    

    I can’t see a more elegant way to solve this problem.

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

Sidebar

Related Questions

I have STI implementation as follows: class Automobile < ActiveRecord::Base end class Car <
For example: I have an implementation (say doSomething()) in Base class. I want all
I downloaded the JSON2.js from https://github.com/douglascrockford/JSON-js/blob/master/json2.js and it does not have implementation for JSON2.stringify()
I don't know either my implementation have bugs or StringBuilder is going wrong. From
I know that it's OK for a pure virtual function to have an implementation.
I have a c++ implementation file (my.cpp) that indirectly includes a header file (b.h)
I have a collection of services that I want to register with Castle Windsor
I have inherited a code base that makes use of the Castle Windsor IoC
I have a class that I want to have access to my IOC container
I have an implementation of lazyDataModel following this tutorial http://uaihebert.com/?p=1120 My code its a

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.