[英]clang vs gcc template child class using forward-declared class in parent
我有一個模板子類(模板typename與父或子不相關,因此不是CRTP繼承的情況),並且父類在私有訪問標簽部分中有一個前向聲明的類。
GCC(在4.9.2和7.2上測試)編譯,但clang(在5.0.0上測試)會抱怨。 這是repro案件:
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 ++標准? 或者標准沒有明確涵蓋這一點?
當訪問模板中的檢查時,gcc有很多錯誤。 看到這個meta-bug 。 你遇到的情況恰好與41437相符:
class A { struct B { B(); }; };
template<typename T> void f() { A::B b; }
void g() { f<int>(); } // gcc says this is okay
鏗鏘是正確的,代碼顯然是不正確的。 Thing
是TestCalss
的私有類,而ThingImpl
不是TestClass
的朋友,因此ThingImpl
嘗試訪問TestClass::Thing
應該是訪問沖突。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.