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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:52:12+00:00 2026-06-14T04:52:12+00:00

There are many folks out there that claim their singleton implementation to be robust

  • 0

There are many folks out there that claim their singleton implementation to be robust and general because it uses metaprogramming constructs.

My goal is to enforce a singleton policy onto a derived class so that I do not have to explicitly (manually) declare the derived class’ constructors as private. I think there’s a way to naively add the instance static variable and the getter as a policy by making the templated singleton a friend of the class you derive. But that’s not at all elegant.

I started with this code, that, among other things, is given as being a correct (i.e. complete) design of a singleton, while it is clearly allowing for multiple instances:

template <class CWrappedClass>
class CSingleton
{
protected:
    static CWrappedClass* ms_instance;
private:
    CSingleton(){}
    CSingleton(const CSingleton& ) {}
    CSingleton& operator = (const CSingleton&) {}

public:
    static CWrappedClass& GetInstance()
    {
        if (ms_instance == NULL)
            ms_instance = new CWrappedClass;
        return *ms_instance;
    }
};

template <class CWrappedClass>
CWrappedClass* CSingleton<CWrappedClass>::ms_instance = NULL;

And a singleton client of this “policy”, using CRTP:

class CThing : public CSingleton<CThing>
{
     // friend class CSingleton<CThing>; // only if ctor is private!
public:
    void DoNothing()
    {
        std::cout<<" Nothing \n";
    }
    CThing()
    {
        std::cout<<" single ";
    }
};

NOTE this is not a correct implementation of a CRTP Singleton policy, it’s merely part of the question!

The code won’t compile as is. The base singleton policy class has its constructor declared private, so it can’t support derived instances unless the child is a friend of this class. People usually make the constructors protected, which means there’s nothing to keep a user from making derived class non-singletonian.

Problem/Question: Is there any mechanism to enforce a singleton policy without having to make the derived class’ constructors private manually?

  • 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-14T04:52:13+00:00Added an answer on June 14, 2026 at 4:52 am

    In case you do wish to use this pattern:

    1. Constructor of the template should be protected, not private, so that the derived class has access to it. Otherwise its own constructor cannot construct the base class.

    2. Derived class can make the template (which is its base class) a friend and have a private constructor.

    Alternatively, have a macro that you implement that actually constructs the instance from the derived class in its compilation unit. When I did use a similar model (by force, fixing code that used singletons and I coudn’t change that, only the way the actual singleton was implemented), I went for this option, which actually went through a boost::once construct in its .cpp file.

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

Sidebar

Related Questions

There are many similar questions, but I didn't find one that gets straight to
There are many questions like this but I can't find one that seems to
There are many threads and samples out there about typeahead ( https://gist.github.com/1866739 ) and
I have a database that uses natural keys (i.e. business oriented keys). Unfortunately, because
How many number of Instance can be there when same singleton class is packaged
There were many questions about C++ template classes which contain static member variables, as
There are many errors here in SO, but this scenario I think its different.
There are many similar questions, however they don't answer the problem of a url
There are many services available for delivering content to users. I am looking for
There are many many posts and an infinite number of questions on how to

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.