【发布时间】:2019-05-28 23:09:30
【问题描述】:
在我正在审查的一些代码中,我遇到了 Clang 和 Gcc 不同意的情况。看了一会儿,我似乎无法弄清楚谁是对的。
免责声明:我知道有更好的单例模式,但这是代码中使用的模式。
注意事项:
Ubuntu 上的 gcc 7.4.0(无错误)
Ubuntu 上的 clang 6.0.0(引发错误)
所有 C++11 后 ISO 版本似乎都存在差异,但我之前没有尝试过。
foo.hh
#include "sing.hh"
class Foo {
public:
Foo();
~Foo();
static Foo *getSingleton(){
return singleton<Foo>::instance();
}
};
foo.cc
include "foo.hh"
//removing this line results in the error for clang disappearing
template<> singleton<Foo>::GetInstance singleton<Foo>::instance = nullptr;
int main(){};
sing.hh
template<typename T>
class singleton{
typedef T *(*GetInstance)(void);
public:
static GetInstance instance;
};
结果:
$ clang++ foo.cc
foo.cc:3:56: error: explicit specialization of 'instance' after instantiation
template<> singleton<Foo>::GetInstance singleton<Foo>::instance = nullptr;
^
./foo.hh:10:32: note: implicit instantiation first required here
return singleton<Foo>::instance();
^
1 error generated.
$ g++ foo.cc <- No Errors
【问题讨论】:
标签: c++ templates gcc clang iso