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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T20:10:41+00:00 2026-06-14T20:10:41+00:00

I implemeted a template singleton class along with static member definition: template <typename T>

  • 0

I implemeted a template singleton class along with static member definition:

template <typename T> class SingletonT
{
public:
    static T *GetInstance()
    {
        if (!s_instance)
        {
            s_instance = new T;
        }
        return s_instance;
    }
private:
    static T *s_instance;
};
template <typename T> T *SingletonT<T>::s_instance = 0;

It compiles fine and works fine.
Now, I’m trying to create a singleton that will use custom function instead of operator new:

template <typename T, T *(*f)()> class PolymorphSingletonT
{
public:
    static T *GetInstance()
    {
        if (!s_instance)
        {
            s_instance = f();
        }
        return s_instance;
    }
private:
    static T *s_instance;
};
template <typename T, T *(*f)()> T *PolymorphSingletonT<T, T *(*f)()>::s_instance = 0;

I get a compile error on the last line saying error C2146: syntax error : missing ')' before identifier 'f'

I use MSVC2005 compiler.

Update
Now I ended up with functor approach:

template <typename T, typename F> class PolymorphSingletonT
{
public:
    static T *GetInstance()
    {
        if (!s_instance)
        {
            s_instance = F::Create();
        }
        return s_instance;
    }
private:
    static T *s_instance;
};
template <typename T, typename F> T *PolymorphSingletonT<T, F>::s_instance = 0;

However, it forces me to wrap my creation function with a functor. It only adds a couple of code lines and works perfectly, but anyway I’m still interested in getting it without functors.

  • 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-14T20:10:42+00:00Added an answer on June 14, 2026 at 8:10 pm

    The last line should be:

    template <typename T, T *(*f)()> T *PolymorphSingletonT<T,f>::s_instance = 0;
    

    Don’t repeat the type signature, use the names you’ve specified.

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

Sidebar

Related Questions

I've got the following (truncated) class declaration: template <typename T> class btree { public:
I implemented class that serves as TcpCLient server. Looks like this: { [Export] public
I implemented the Strategy design pattern using an Abstract template class, and two subclasses.
I have implemented two methods of how I think a Singleton class should be
The STL functors are implemented like this: template<class T> struct less{ bool operator()(T const&
Is it possible to write template definition for this data type? myclass<int, myclass<int> >
I am writing a template class for an array of objects, call it arrayobjclass,
I implemented a template base class for observer pattern, template<class T> class ActionListener {
Continuing from my last question C++ template class map I have implemented the function
Is it implemented already, because this does not compile: (using gcc 4.7.2) template <typename...

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.