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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:41:17+00:00 2026-06-02T15:41:17+00:00

Summary: How can I create a singleton mixin in C++? I am trying to

  • 0

Summary: How can I create a singleton mixin in C++? I am trying to avoid copying the same get_instance() function, the private constructor, etc. But I can’t figure out a way to make this a mixin, because the static instance will be shared by everything that inherits from the mixin.

It’s easy to make each derived class a singleton, but is there a way to do it without duplicating code? Thank a lot for your help, I’m stumped.

Code:
I am writing a program with a Registry class for looking up objects by name.

#include <string>
#include <memory>
#include <map>
#include <string>
#include <assert.h>

template <typename T>
class Registry
{
private:
  // make private so that the class can't be instantiated and must be used via get_instance
  Registry() {}
protected:
  std::map<std::string, std::shared_ptr<T> > name_to_object_ptr;
public:
  static Registry<T> & get_instance()
  {
    static Registry<T> instance;
    return instance;
  }
  void register_name(const std::string & name, T*obj_ptr)
  {
    assert( name_to_object_ptr.count(name) == 0 );
    name_to_object_ptr[name] = std::shared_ptr<T>(obj_ptr);
  }
  const std::shared_ptr<T> & lookup_name(const std::string & name)
  {
    assert( name_to_object_ptr.count(name) > 0 );
    return name_to_object_ptr[name];
  }
  int size() const
  {
    return name_to_object_ptr.size();
  }
};

My Registry class is a singleton; it must be a singleton (so that registered objects will not disappear).

class DerivedRegistryA : public Registry<int>
{
};

class DerivedRegistryB : public Registry<int>
{
};

int main()
{
  DerivedRegistryA::get_instance().register_name(std::string("one"), new int(1));
  std::cout << DerivedRegistryA::get_instance().size() << std::endl;
  DerivedRegistryA::get_instance().register_name(std::string("two"), new int(2));
  std::cout << DerivedRegistryA::get_instance().size() << std::endl;
  DerivedRegistryA::get_instance().register_name(std::string("three"), new int(3));
  std::cout << DerivedRegistryA::get_instance().size() << std::endl;

  DerivedRegistryB::get_instance().register_name(std::string("four"), new int(4));
  std::cout << DerivedRegistryB::get_instance().size() << std::endl;

  return 0;
}

Output:

1
2
3
4

Desired output:

1
2
3
1

  • 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-02T15:41:19+00:00Added an answer on June 2, 2026 at 3:41 pm

    What lionbest said sounds right. Here is a related idea that is more similar to your original design.

    You declare a template class that works in a way similar to a factory for Registry objects. I called it RegAccess:

    template <typename RegType>
    class RegAccess
    {
    public:
      static RegType & get_instance()
      {
        static RegType instance;
        return instance;
      }
    };
    

    To make it work, you:

    • Declare RegAccess<Registry<T> > a friend of Registry<T> (to be able to do that you need to make sure it’s defined someplace before Registry<T>)
    • Make the constructor of Registry protected, rather than private (so constructors of the derived classes can use it implicitly)
    • Remove the get_instance method from the Registry<T> class definition

    And then your main program becomes:

    int main()
    {
      RegAccess<DerivedRegistryA>::get_instance().register_name(std::string("one"), new int(1));
      std::cout << RegAccess<DerivedRegistryA>::get_instance().size() << std::endl;
      RegAccess<DerivedRegistryA>::get_instance().register_name(std::string("two"), new int(2));
      std::cout << RegAccess<DerivedRegistryA>::get_instance().size() << std::endl;
      RegAccess<DerivedRegistryA>::get_instance().register_name(std::string("three"), new int(3));
      std::cout << RegAccess<DerivedRegistryA>::get_instance().size() << std::endl;
    
      RegAccess<DerivedRegistryB>::get_instance().register_name(std::string("four"), new int(4));
      std::cout << RegAccess<DerivedRegistryB>::get_instance().size() << std::endl;
    
      return 0;
    }
    

    When I tested this, it generated the desired output.

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

Sidebar

Related Questions

is there a way i can truncate a post. eg. create a blog summary.
I'm trying out some code from a singleton base class article . But when
Does anyone know of a function that can create an lm object given a
Summary How can you make ant repeatedly generate byte-identical jar files from the same
Summary : if I create an object in a constructor initialiser, how do I
How can I write this code more cleanly/concisely? /// <summary> /// Creates a set
Summary Can you explain the reasoning behind the syntax for encapsulated anonymous functions in
SUMMARY How can I make my GUI application run on windows startup on a
Summary A parent can have many children. How do you write a service such
Where can I get a summary of differences between ActionScript 3.0 strict and standard

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.