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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:24:34+00:00 2026-05-30T21:24:34+00:00

I am trying to initialize a static object without success. The purpose is to

  • 0

I am trying to initialize a static object without success. The purpose is to automatically register a factory class in a repository (which is a singleton).

I’ve already had a look at: How to force a static member to be initialized?

One of the comments says that (there is also an example that I’ve followed):

I read it up in the C++ standard (14.7.1): Unless a member of a class template or a member template has been explicitly instantiated or explicitly specialized, the specialization of the member is implicitly instantiated when the specialization is referenced in a context that requires the member definition to exist; in particular, the initialization (and any associated side-effects) of a static data member does not occur unless the static data member is itself used in a way that requires the definition of the static data member to exist.

So I’m trying to do something similar but I haven’t manage to force the object initialization. Here is the code. I don’t know what I’m missing. This is the template I’m using.

namespace my_lib
{
    template <typename T>
    struct FactoryHelper
    {
        FactoryHelper ();
        static FactoryHelper<T> _helper;
    };
}

And this is the macro that the user of the library would use to define the factory class and, at the same time, register an object in the repository:

#define CREATE_FACTORY(ClassName)\
namespace my_lib\
{\
    class ClassName##Factory;\
    template<> FactoryHelper<ClassName##Factory>::FactoryHelper () { std::cout << "object initialized!" << std::endl; }\
    template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper;\
    struct ClassName##Factory : public FactoryBase<ClassName> {\
      ...\
    };\
} 

The previous code is defined in a header file (Factory.h).

In a .cpp file (Example.cpp), I have:

CREATE_FACTORY(UnitTestExample)
...

When I execute the program, I cannot see the message that the constructor prints when it is invoked. Any help is more than welcome.

Thanks in advance.

  • 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-05-30T21:24:36+00:00Added an answer on May 30, 2026 at 9:24 pm

    This is a tricky area of C++. What you’ve done is to try to define the static member here:

    template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper;\
    

    but this is actually a declaration and not a definition. For C++ to treat it as a definition you have to pass something to the constructor. Typically, this is the value you want to initialize it to:

    template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper = FactoryHelper<ClassName##Factory>();\
    

    But in your case, you want this to be a singleton, so you probably don’t want it to be copyable. In that case, you need some dummy parameter:

    template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper(0);\
    

    and you have to modify your constructor appropriately:

    template<> FactoryHelper<ClassName##Factory>::FactoryHelper (int) { std::cout << "object initialized!" << std::endl; }\
    

    Here is the complete working example:

    #include <iostream>
    
    namespace my_lib
    {
        template<typename> struct FactoryBase { };
        template <typename T>
        struct FactoryHelper
        {
            FactoryHelper (int);
            static FactoryHelper<T> _helper;
        };
    }
    
    #define CREATE_FACTORY(ClassName)\
    namespace my_lib\
    {\
        class ClassName##Factory;\
        template<> FactoryHelper<ClassName##Factory>::FactoryHelper (int) { std::cout << "object initialized!" << std::endl; }\
        template<> FactoryHelper<ClassName##Factory> FactoryHelper<ClassName##Factory>::_helper(0);\
        struct ClassName##Factory : public FactoryBase<ClassName> {\
        };\
    } 
    
    struct UnitTestExample {
    };
    
    CREATE_FACTORY(UnitTestExample);
    
    int main(int argc,char **argv)
    {
      return 0;
    }
    

    That said, using some of the suggestions in the other answers may be a better design decision.

    More information on the explicit specialization declaration vs. definition can be found here: static member initialization for specialized template class

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

Sidebar

Related Questions

how to initialize a private static member of a class in java. trying the
I'm trying to initialize a static class, with an argument, and then run some
I am trying to initialize an expensive object, through .NET's Lazy class, that can
I'm trying to write a simple program in VC++ which will just initialize the
I am trying to initialize my controls in Silverlight. I am looking for something
I'm trying to initialize string with iterators and something like this works: ifstream fin(tmp.txt);
Edited again because it originally wasn't clear that I'm trying to initialize the arrays
I have a list string tag. I am trying to initialize a dictionary with
How do I initialize a 2D array in Perl? I am trying the following
I thought the purpose of mkbundle2 was to allow a machine without mono installed

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.