在最初于 2001 年编写的大型 C++ 代码库中,大量使用了 Singleton 类,其定义如下:
template <class T>
struct Singleton
{ T *Instance() { return _instance; }
...
static T *_instance;
};
#define INIT_SINGLETON(T) \
T * Singleton <T>::_instance = ((T*)0UL)
现在,使用我必须使用的 GCC 4.7.7,使用默认的 c++98 标准,并在测试文件中使用 INIT_SINGLETON(structX),我得到:
$ g++ -std=c++98 -c /tmp/ts.cpp
/tmp/ts.cpp:11: error: too few template-parameter-lists
代码最后在 Linux RHEL3 上使用 GCC 3.4.3 编译成功 - 我正在尝试将其移植到 RHEL6 (GCC 4.4.4 / 4.7.7)。
自从我使用 C++98 代码以来已经有一段时间了,虽然我从 @ 1994 开始使用 C++,但今天我似乎无法理解这个问题。
谁能解释一下为什么会发生这个错误以及如何避免它?
应 SM 和 NathanOliver 的要求,这是 /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 给出错误:
$ g++ -std=c++98 -Isonim/MRFP/inc -c /tmp/ts.cpp
/tmp/ts.cpp:15: error: too few template-parameter-lists