我有一个模板子类(模板类型名称与父类或子类无关,因此不是CRTP继承情况),并且父类在私有访问标签部分中有一个前向声明的类。
GCC (测试4.9.2和7.2)编译,但clang (测试5.0.0)会抱怨。这就是复制案例:
class TestClass
{
public:
// Forward-declare as public to make compile with clang
// class Thing;
private:
// GCC compiles fine with forward-declaration as private but clang gives error
class Thing;
friend class Thing;
void NotifyOfThing();
};
class TestClass::Thing
{
public:
static void NotifyOfThing() {}
};
template <typename Unrelated>
class ThingImpl final : public Unrelated
{
private:
void handleThing()
{
TestClass::Thing::NotifyOfThing();
}
};
int main() {
ThingImpl<TestClass> implementation;
}Clang抛出一个错误:
25 : <source>:25:20: error: 'Thing' is a private member of 'TestClass'
TestClass::Thing::NotifyOfThing();
^
8 : <source>:8:11: note: declared private here
class Thing;
^
1 error generated.
Compiler exited with result code 1然而,GCC接受了这一点。
现在,如果我删除模板声明,并使ThingImpl成为一个非模板类,GCC也会有同样的抱怨,Thing是私有的。
有人能解释为什么会出现这种情况,哪一个更符合C++标准?还是标准没有明确地涵盖这一点?
发布于 2017-10-16 03:12:01
https://stackoverflow.com/questions/46762007
复制相似问题