C++ XCode警告;变量'的实例化;辛格尔顿<;Foo>;::_实例';此处需要,但没有可用的定义
旧代码库具有以下构造:C++ XCode警告;变量'的实例化;辛格尔顿<;Foo>;::_实例';此处需要,但没有可用的定义,c++,xcode,macos-sierra,C++,Xcode,Macos Sierra,旧代码库具有以下构造: template< typename T > class Singleton { private: static T* _instance; public: inline static T& instance() { if (_instance == 0) { // warning here _instance = new T; } return *_instanc
template< typename T >
class Singleton {
private:
static T* _instance;
public:
inline static T& instance() {
if (_instance == 0) { // warning here
_instance = new T;
}
return *_instance;
}
};
但这无助于编译。在定义Foo之前,是否有方法提供Singleton::\u实例的定义
XCode 9.2 Mac OS X 10.12.6在头文件中添加
template <typename T> T* Singleton<T>::_instance = nullptr;
template T*Singleton::_instance=nullptr;
这仍然是一个越轨的定义,但与特定的专业化没有联系。然后,您应该能够删除该行
template<> Foo* Singleton<Foo>::_instance = nullptr;
template Foo*Singleton::_instance=nullptr;
因为上面的非专门化定义已经对所有实例化执行了相同的操作。您知道,我可以发誓我已经尝试过了(以及将内联方法定义移出了内联方法定义),但它没有解决警告问题。然而,我按照你的建议重新实施了。。。警告消失了。嘘。
template <typename T> T* Singleton<T>::_instance = nullptr;
template<> Foo* Singleton<Foo>::_instance = nullptr;