下面的代码片段由gcc,icc和msvc(最新问题)编译得很好,但是在标记的行中与<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!
}
在此上下文中,非模板类的静态模板成员与自由模板函数之间可能有什么区别?
答案 0 :(得分:1)
我发现了一个针对Clang "Access to a public template alias declaration that refers to friend's private nested type is not allowed"的错误报告,该错误似乎与此问题相似,因为它与访问类的私有成员的朋友(如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.