[英]Template member of a non-template class as a friend
下面的代碼片段由gcc,icc和msvc(最新問題)編譯得很好,但是與<source>:6:9: error: calling a private constructor of class 'B<int>'
在標記行中<source>:6:9: error: calling a private constructor of class 'B<int>'
。 然而它適用於免費模板功能,如代碼所示:
struct A {
template<class T>
static void create () {
T();
}
};
template<class T>
void create() {
T();
}
template<typename T>
struct B {
friend void A::create<B>();
friend void create<B>();
private:
B() = default;
};
int main() {
A::create<B<int>>(); // clang trips here
create<B<int>>(); // fine!
}
在此上下文中,非模板類的靜態模板成員與自由模板函數之間可能有什么區別?
我發現了一個針對Clang報告的錯誤, “不允許訪問引用朋友私有嵌套類型的公共模板別名聲明” ,這與此問題類似,因為它與朋友(結構中的模板,如OP中)相關班級的私人成員。
失敗的測試用例是:
struct FooAccessor
{
template <typename T>
using Foo = typename T::Foo;
};
class Type
{
friend struct FooAccessor;
using Foo = int;
};
int main()
{
FooAccessor::Foo<Type> t;
}
結果是:
$ clang++ test.cpp -std=c++11
test.cpp:4:5: error: 'Foo' is a private member of 'Type'
using Foo = typename T::Foo;
^
test.cpp:11:11: note: implicitly declared private here
using Foo = int;
^
1 error generated.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.