1

In a large C++ code base, originally written @ 2001, a Singleton class is used heavily, and is defined like:

   template <class T> 
   struct Singleton 
   { T *Instance() { return _instance; }
   ... 
     static T *_instance;
   }; 
   #define INIT_SINGLETON(T) \
    T * Singleton <T>::_instance = ((T*)0UL)

Now, with GCC 4.7.7, which I MUST use, with the default c++98 standard, and a use of INIT_SINGLETON(structX) in a test file, I get:

   $ g++ -std=c++98 -c /tmp/ts.cpp 
   /tmp/ts.cpp:11: error: too few template-parameter-lists

The code last compiled OK with GCC 3.4.3 on Linux RHEL3 - I am trying to port it to RHEL6 (GCC 4.4.4 / 4.7.7).

It's been a while since I used C++98 code, though I've used C++ since @ 1994 , I can't seem to get my head around this one today.

Could anyone please shed some light on why this error occurs and how to avoid it ?

In response to S.M.'s & NathanOliver's request, this is /tmp/ts.cpp:

    template <class T> 
    struct Singleton 
    { T * Instance() { return _instance; } 
      T * _instance; 
    }; 

    struct ab_s { int a, b; }; 

    typedef Singleton<ab_s> S_ab_s_t; 

    ab_s* f(S_ab_s_t sa)
    { return sa.Instance(); 
    }

    ab_s * Singleton<ab_s> :: _instance = NULL; 

GCC 4.7.7 gives error:

    $ g++ -std=c++98 -Isonim/MRFP/inc -c /tmp/ts.cpp  
    /tmp/ts.cpp:15: error: too few template-parameter-lists
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
JVD
  • 645
  • 1
  • 7
  • 17
  • `Singleton ` -> `Singleton`? – KamilCuk Aug 19 '21 at 15:13
  • could be a parsing issue. Does the error go away if you remove the white space between the name and parameter and have `T * Singleton::_instance = 0`? – NathanOliver Aug 19 '21 at 15:14
  • 1
    Please post the [mcve]. You already have it: tmp/ts.cpp:11, the file seems to be small. – 273K Aug 19 '21 at 15:16
  • no - I am paraphrasing the actual code, which has no space there. – JVD Aug 19 '21 at 15:16
  • 2
    We can't help you debug code that isn't what you are showing us. Please edit this to have a [mre]. – NathanOliver Aug 19 '21 at 15:18
  • The code you’ve now posted is *completely different* from the original code: `Singleton` isn’t a template, its `_instance` member isn’t `static`, and there’s no macro. – Konrad Rudolph Aug 19 '21 at 15:27
  • Note on formatting: you must put ``` in a separate line, otherwise it will consume anything in the same line looking for formatting specification. – Yksisarvinen Aug 19 '21 at 15:29
  • 2
    Why mess with a macro if you want the same initialization for all specializations? Just define `template T* Singleton::_instance = NULL;` right under the class template and GCC will sort it out. – StoryTeller - Unslander Monica Aug 19 '21 at 15:30
  • @StoryTeller: I don't think I can do that and remain C++98 / C++03 compatible. Will try. – JVD Aug 19 '21 at 15:34

1 Answers1

1

Template specialization requires template<>.

#define INIT_SINGLETON(T) \
    template<> \
    T * Singleton<T>::_instance = ((T*)0UL)

The compiling example

template <class T> 
struct Singleton 
{
  T *Instance() { return _instance; }
  static T *_instance;
}; 

#define INIT_SINGLETON(T) \
  template<> \
  T * Singleton<T>::_instance = ((T*)0UL)

struct structX{};

INIT_SINGLETON(structX);

To the updated question:

template<>
ab_s * Singleton<ab_s> :: _instance = NULL; 
273K
  • 29,503
  • 10
  • 41
  • 64